From 5de21c7fa2bdd5cd50c4f62ba848af54589167d0 Mon Sep 17 00:00:00 2001 From: Vincent Driessen Date: Tue, 12 Apr 2016 15:55:01 +0200 Subject: Support "root" as a special value in .diff() calls This enabled getting diff patches for root commits. --- git/diff.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'git/diff.py') diff --git a/git/diff.py b/git/diff.py index 062220df..eada73b9 100644 --- a/git/diff.py +++ b/git/diff.py @@ -49,6 +49,7 @@ class Diffable(object): If None, we will be compared to the working tree. If Treeish, it will be compared against the respective tree If Index ( type ), it will be compared against the index. + If the string 'root', it will compare the empty tree against this tree. It defaults to Index to assure the method will not by-default fail on bare repositories. @@ -87,10 +88,15 @@ class Diffable(object): if paths is not None and not isinstance(paths, (tuple, list)): paths = [paths] - if other is not None and other is not self.Index: - args.insert(0, other) + diff_cmd = self.repo.git.diff if other is self.Index: - args.insert(0, "--cached") + args.insert(0, '--cached') + elif other == 'root': + args.insert(0, '--root') + diff_cmd = self.repo.git.diff_tree + elif other is not None: + args.insert(0, other) + diff_cmd = self.repo.git.diff_tree args.insert(0, self) @@ -101,7 +107,7 @@ class Diffable(object): # END paths handling kwargs['as_process'] = True - proc = self.repo.git.diff(*self._process_diff_args(args), **kwargs) + proc = diff_cmd(*self._process_diff_args(args), **kwargs) diff_method = Diff._index_from_raw_format if create_patch: -- cgit v1.2.1