From 7531db5d7ddc92253125caafbd9f935005496bad Mon Sep 17 00:00:00 2001 From: James Ascroft-Leigh Date: Sun, 22 Feb 2009 20:31:59 +0000 Subject: [PATCH] Initial version of git-meld-commit This tool (inefficiently) shows the the changes between a commit's tree and the parent commit(s)'s tree. It shows the changes using meld so the full context of the change can be seen. --- git-meld-commit | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100755 git-meld-commit diff --git a/git-meld-commit b/git-meld-commit new file mode 100755 index 0000000..d58da13 --- /dev/null +++ b/git-meld-commit @@ -0,0 +1,101 @@ +#!/usr/bin/env python + +# Copyright (c) 2009 James Ascroft-Leigh +# +# Permission is hereby granted, free of charge, to any person +# obtaining a copy of this software and associated documentation +# files (the "Software"), to deal in the Software without +# restriction, including without limitation the rights to use, +# copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following +# conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +# OTHER DEALINGS IN THE SOFTWARE. + +"""\ +%prog [options] COMMIT + +Shows the difference between the tree from COMMIT and the tree from +the parent of COMMIT. Invokes meld once for each parent. +""" + +# test cases: +# initial: (cd ~/git/nacl-build && ~/git/git-meld-toys/git-meld-commit \ +# 8816f9f4ea28583d6dfa1fd96d3448977f6e2299) +# commit: (cd ~/git/nacl-build && ~/git/git-meld-toys/git-meld-commit \ +# 707c127f621bf308f1e1a8526e79d5dc3888483c) +# merge: (cd ~/git/git && ~/git/git-meld-toys/git-meld-commit \ +# 2d602e9179c7eb1a31a0abf41283c132e26a61af) + +import optparse +import os +import subprocess +import shutil +import sys +import tempfile + + +def parse_argv(prog, argv): + parser = optparse.OptionParser(__doc__, prog=prog) + options, args = parser.parse_args(argv) + if len(args) == 0: + commit_spec = "HEAD" + else: + commit_spec = args.pop(0) + if len(args) > 0: + parser.error("Unexpected: %r" % (args,)) + return (commit_spec,) + + +def dump_tree(tree_ish, destination): + os.mkdir(destination) + git = subprocess.Popen(["git", "archive", "--format=tar", tree_ish], + stdout=subprocess.PIPE) + tar = subprocess.Popen(["tar", "-C", destination, "-x"], + stdin=git.stdout) + tar.communicate() + git.communicate() + assert git.returncode == 0, git.returncode + assert tar.returncode == 0, tar.returncode + + +def meld(lhs, rhs): + work_dir = tempfile.mkdtemp(prefix="git-meld-commit-") + try: + lhs_path = os.path.join(work_dir, "lhs", lhs) + rhs_path = os.path.join(work_dir, "rhs", rhs) + os.mkdir(os.path.dirname(lhs_path)) + os.mkdir(os.path.dirname(rhs_path)) + dump_tree(lhs, lhs_path) + dump_tree(rhs, rhs_path) + subprocess.check_call(["meld", lhs_path, rhs_path]) + finally: + shutil.rmtree(work_dir) + + +def main(commit_spec): + cmd = ["git", "show", "--quiet", "--pretty=format:%P", commit_spec] + git = subprocess.Popen(cmd, stdout=subprocess.PIPE) + stdout, stderr = git.communicate() + assert git.returncode == 0, git.returncode + parents = stdout.rstrip("\n").split(" ") + if parents == [""]: + parents = [] + for parent in parents: + meld(commit_spec, parent) + + +if __name__ == "__main__": + sys.exit(main(*parse_argv(sys.argv[0], sys.argv[1:]))) + -- 2.11.4.GIT