diff --git a/hgext3rd/sparse.py b/hgext3rd/sparse.py --- a/hgext3rd/sparse.py +++ b/hgext3rd/sparse.py @@ -9,7 +9,7 @@ """ from mercurial import util, cmdutil, extensions, context, dirstate, commands -from mercurial import localrepo, error, hg, pathutil, registrar +from mercurial import localrepo, error, hg, pathutil, registrar, patch from mercurial import match as matchmod from mercurial import merge as mergemod from mercurial.node import nullid @@ -342,6 +342,9 @@ extensions.wrapfunction(dirstate.dirstate, func, _wrapper) def _setupdiff(ui): + entry = commands.table['^diff'] + entry[1].append(('s', 'sparse', None, + 'only show changes in files in the sparse config')) # wrap workingfilectx's data function to return the data for files # outside the sparse checkout by fetching from the working copy parent. def workingfilectxdata(orig, self): @@ -353,6 +356,27 @@ return basectx[self._path].data() extensions.wrapfunction(context.workingfilectx, 'data', workingfilectxdata) + # wrap trydiff to filter diffs if '--sparse' is set + def trydiff(orig, repo, revs, ctx1, ctx2, modified, added, removed, + copy, getfilectx, opts, losedatafn, prefix, relroot): + sparsematch = repo.sparsematch() + modified = filter(sparsematch, modified) + added = filter(sparsematch, added) + removed = filter(sparsematch, removed) + copy = dict((d, s) for d, s in copy.items() if sparsematch(s)) + return orig(repo, revs, ctx1, ctx2, modified, added, removed, + copy, getfilectx, opts, losedatafn, prefix, relroot) + def diff(orig, ui, repo, *pats, **opts): + issparse = bool(opts.get('sparse')) + if issparse: + extensions.wrapfunction(patch, 'trydiff', trydiff) + try: + orig(ui, repo, *pats, **opts) + finally: + if issparse: + extensions.unwrapfunction(patch, 'trydiff', trydiff) + extensions.wrapcommand(commands.table, 'diff', diff) + def _wraprepo(ui, repo): class SparseRepo(repo.__class__): def readsparseconfig(self, raw): diff --git a/tests/test-sparse-diff.t b/tests/test-sparse-diff.t --- a/tests/test-sparse-diff.t +++ b/tests/test-sparse-diff.t @@ -4,28 +4,102 @@ > [extensions] > sparse=$TESTDIR/../hgext3rd/sparse.py > EOF - $ mkdir a b - $ echo a1 > a/a - $ echo b1 > b/b - $ hg add a/a b/b - $ hg commit -m "add files" - $ echo a2 > a/a - $ echo b2 > b/b - $ hg commit -m "modify files" - $ hg sparse --exclude b + $ mkdir show hide + $ echo show-modify-1 > show/modify + $ echo show-remove-1 > show/remove + $ echo hide-modify-1 > hide/modify + $ echo hide-remove-1 > hide/remove + $ echo show-moveout > show/moveout + $ echo show-movein > hide/movein + $ hg add show/modify show/remove hide/modify hide/remove show/moveout hide/movein + $ hg commit -m "first revision" + $ echo show-modify-2 > show/modify + $ echo show-add-2 > show/add ; hg add show/add + $ hg rm show/remove + $ echo hide-modify-2 > hide/modify + $ echo hide-add-2 > hide/add ; hg add hide/add + $ hg rm hide/remove + $ hg mv hide/movein show/movein + $ hg mv show/moveout hide/moveout + $ hg commit -m "second revision" + $ hg sparse --exclude hide Run diff. This should still show the file contents of excluded files (and should not crash). - $ hg diff -r ".^" - diff -r 45479a47b024 a/a - --- a/a/a Thu Jan 01 00:00:00 1970 +0000 - +++ b/a/a Thu Jan 01 00:00:00 1970 +0000 + $ hg diff -r ".^" --git + diff --git a/hide/add b/hide/add + new file mode 100644 + --- /dev/null + +++ b/hide/add + @@ -0,0 +1,1 @@ + +hide-add-2 + diff --git a/hide/modify b/hide/modify + --- a/hide/modify + +++ b/hide/modify + @@ -1,1 +1,1 @@ + -hide-modify-1 + +hide-modify-2 + diff --git a/show/moveout b/hide/moveout + rename from show/moveout + rename to hide/moveout + diff --git a/hide/remove b/hide/remove + deleted file mode 100644 + --- a/hide/remove + +++ /dev/null + @@ -1,1 +0,0 @@ + -hide-remove-1 + diff --git a/show/add b/show/add + new file mode 100644 + --- /dev/null + +++ b/show/add + @@ -0,0 +1,1 @@ + +show-add-2 + diff --git a/show/modify b/show/modify + --- a/show/modify + +++ b/show/modify @@ -1,1 +1,1 @@ - -a1 - +a2 - diff -r 45479a47b024 b/b - --- a/b/b Thu Jan 01 00:00:00 1970 +0000 - +++ b/b/b Thu Jan 01 00:00:00 1970 +0000 + -show-modify-1 + +show-modify-2 + diff --git a/hide/movein b/show/movein + rename from hide/movein + rename to show/movein + diff --git a/show/remove b/show/remove + deleted file mode 100644 + --- a/show/remove + +++ /dev/null + @@ -1,1 +0,0 @@ + -show-remove-1 + +Run diff --sparse. This should only show files within the sparse profile. + + $ hg diff --sparse --git -r ".^" + diff --git a/show/add b/show/add + new file mode 100644 + --- /dev/null + +++ b/show/add + @@ -0,0 +1,1 @@ + +show-add-2 + diff --git a/show/modify b/show/modify + --- a/show/modify + +++ b/show/modify @@ -1,1 +1,1 @@ - -b1 - +b2 + -show-modify-1 + +show-modify-2 + diff --git a/show/movein b/show/movein + new file mode 100644 + --- /dev/null + +++ b/show/movein + @@ -0,0 +1,1 @@ + +show-movein + diff --git a/show/moveout b/show/moveout + deleted file mode 100644 + --- a/show/moveout + +++ /dev/null + @@ -1,1 +0,0 @@ + -show-moveout + diff --git a/show/remove b/show/remove + deleted file mode 100644 + --- a/show/remove + +++ /dev/null + @@ -1,1 +0,0 @@ + -show-remove-1