Skip to content

Instantly share code, notes, and snippets.

@ScottDuckworth
Created January 24, 2014 13:54
Show Gist options
  • Save ScottDuckworth/8597653 to your computer and use it in GitHub Desktop.
Save ScottDuckworth/8597653 to your computer and use it in GitHub Desktop.
Finding last commit of all files in git directory
#!/usr/bin/env python
import pygit2
import stat
import sys
if len(sys.argv) < 3:
print("Usage: %s repo-path rev [path]" % sys.argv[0])
sys.exit(1)
repopath = sys.argv[1]
rev = sys.argv[2]
if len(sys.argv) >= 4:
path = sys.argv[3]
repo = pygit2.Repository(repopath)
commit = repo.revparse_single(rev)
parents = commit.parents
search = set()
found = {}
for entry in commit.tree:
search.add(entry.name)
walker = repo.walk(commit.oid, pygit2.GIT_SORT_TOPOLOGICAL)
for c in walker:
if not c.parents:
for f in search:
found[f] = c
search = None
break
# skip merges
if len(c.parents) > 1:
continue
# get diff from my first parent
p = c.parents[0]
diff = repo.diff(p, c)
for patch in diff:
f = patch.new_file_path.split('/', 1)[0]
if f in search:
found[f] = c
search.remove(f)
# we're done if we've found the latest commit for all files
if not search:
break
if search:
print("NOT FOUND: %s" % sorted(search))
assert(False)
for f in sorted(found):
c = found[f]
print("%-23s %s %s" % (f, c.hex, c.message.splitlines()[0]))
#!/bin/sh
GIT_DIR="$1"
if [ -d "$GIT_DIR/.git" ]; then
GIT_DIR="$GIT_DIR/.git"
fi
export GIT_DIR
rev="$2"
for f in `git ls-tree --name-only $rev`; do
log="`git log --pretty=format:'%H %s' -n1 "$rev" -- "$f"`"
printf "%-23s %s\n" "$f" "$log"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment