Skip to content

Instantly share code, notes, and snippets.

@bradparks
Forked from padawin/README.md
Created April 8, 2019 20:48
Show Gist options
  • Save bradparks/b91abf4e901339090836eed26ea7ad7c to your computer and use it in GitHub Desktop.
Save bradparks/b91abf4e901339090836eed26ea7ad7c to your computer and use it in GitHub Desktop.
Stackoverflow #55496873

Commits log

Lists the last commit affecting a file across multiple repositories

Script written in answer of [https://stackoverflow.com/questions/55496873/git-find-last-time-a-file-was-modified-across-multiple-repos]

Requirements

Python3.6 or above is needed, along with the requests package (included in requirements.txt).

To install the requirements, you need to run:

pip install -r requirements.txt

Usage

Edit repos.txt to define the file you want to log and the list of repos you want to go through. The format is as follow:

  • Path of the file, relative to the repository in the first line
  • Each repository on the following lines

Example:

README.md
padawin/dotfiles
padawin/site
padawin/floodit-clone

Each line of repos.txt must be terminated with a Unix end of line (\n).

Then, run the script:

python3.6 log.py

The script will first output the erroring lines, then each commit line for each repo, sorted from the latest to the oldest.

Note

The commits are sorted by author date by default. If you want them sorted by committer date, call the script with committer as argument:

python3.6 log.py committer
import json
import requests
import sys
GITHUB = "https://api.github.com/repos/{repo}/commits?path={file_path}"
if __name__ == "__main__":
args = set(sys.argv[1:])
commits = list()
with open('repos.txt', 'r') as content_file:
content = content_file.read().split("\n")
f = content[0]
# Loop through the repos
for repo in content[1:]:
repo = repo.strip()
# If the line is empty, skip
if not repo:
continue
response = requests.get(GITHUB.format(repo=repo, file_path=f))
# If the response is not ok, skip
if response.status_code != 200:
print(f"{f} in {repo} ignored")
try:
first_commit = json.loads(response.content)[0]
except IndexError:
print(f"{f} not found in repo {repo}")
continue
# Get some needed data
sha = first_commit["sha"]
committer_date = first_commit["commit"]["committer"]["date"]
author_date = first_commit["commit"]["author"]["date"]
message = first_commit["commit"]["message"]
date = committer_date if 'committer' in args else author_date
# Format the output
commits.append(f"{date} {sha} {message} {repo}")
# Sort and print the result
commits.sort(reverse=True)
for commit in commits:
print(commit)
README.md
padawin/dotfiles
padawin/site
padawin/floodit-clone
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment