Reputation: 528
My goal is to offer a "backup repo" option in a UI only if the most recent backup is no longer valid, so I need an efficient way to determine if any change at all has been made to the repo.
My first thought is to hash together the modification times of every directory in the repo, which I would compare with the same value computed on the backup. Would that work? Is there a better way?
Upvotes: 1
Views: 3136
Reputation: 8237
You really just want to see if (after fetching) your remote tracking branch origin/branch is the same as your local tracking branch, so just check the SHA's of such (followed by a merge to catch you up). So you might just do a rev-parse on origin/branch and compare that to the rev-parse of branch, since it doesn't matter by how many commits or what the changes where, just that there were changes (or a git log --oneline origin/branch..branch piped to a wc -l, kind of thing).
Your idea of the hash over the entire directory is similar to how the SHA of the commit is actually computed, so just using the SHA achieves that. Note that my suggestion would fail if someone did a commit and then a revert and so you wound up with the same contents, the SHAs would change with each successive commit (since they also contain the commit info as well as the parent SHA), but I would think that its an unlikely scenario and not one you need to optimize for.
So my approach assumes you have a local repo that you are cloning off of the remote. If not, or you don't want to be pulling down the whole remote repo, you can do a shallow clone to get get the tip and remember what the tip was before.
However, a better approach is to use git ls-remote
and parse that output and remember its state and compare it the next time you run it.
NAME
git-ls-remote - List references in a remote repository
SYNOPSIS
git ls-remote [--heads] [--tags] [--upload-pack=<exec>]
[--exit-code] <repository> [<refs>...]
DESCRIPTION
Displays references available in a remote repository along with the associated commit IDs.
You will see output like the following:
From https://github.com/foobar/myRepo.git
3133d81f8cca51c73c92aeeaa1c762b177683456 HEAD
3133d81f8cca51c73c92aeeaa1c762b177683456 refs/heads/master
So you see it lists the remote's HEAD as well as its refs and its a simple matter of parsing that and saving it.
Upvotes: 1
Reputation: 142542
... My first thought is to hash together the modification times of every directory in the repo, which I would compare with the same value computed on the backup. Would that work? Is there a better way?
yep, you can check to see if there are any pending changes from the server:
# grab all latest changes
git fetch
# check to see if there are any changes
git diff <origin/branch> ^branch
# or
git diff ^<branch> origin/branch
One is for pull and second one is for push
Another way is to fetch and tan count the number of commits in each branch assuming you are not doing an interactive rebase [squash].
git rev-list --count
Upvotes: 0
Reputation: 1220
You can parse the output of...
git fetch --dry-run origin
Upvotes: 0