[darcs-users] Getting a full diff for pulled patches

Lele Gaifax lele at nautilus.homeip.net
Thu Jul 26 07:40:54 UTC 2007


At Wed, 25 Jul 2007 17:46:34 -0700,
David Roundy wrote:
> And I'd like to have a "darcs query cat" subcommand that just dumps the
> contents of a file, much like annotate, but a more focussed command, and
> that doesn't add the annotations (which would make it faster).

This is what the trac plugin for darcs uses as a poor man workaround:

    def simulate_cat( self, hash, path, cat_cmd ) :
        # This is an ugly hack to simulate 'darcs cat'.
        # We basically do a 'darcs diff' giving 'cat %2'
        # as the diff command!!
        # When using --diff-command, darcs prints:
        #  Running command <blah blah>...
        #  Hit return to move on...
        # So we need to pass a dummy '\n' as input to the command
        # and remove the prompt messages from the output.
        cmd = 'diff --diff-command "%s %%2"' % cat_cmd
        cmd += ' --to-match "hash %s" "%s"' % (hash,path)
        data = self._run( cmd, input='\n' )
        # Remove cruft added by darcs...
        marker = 'Hit return to move on...'
        pos = data.find( marker )
        data = data[pos+len(marker):]
        # Remove extra newline at the end
        if data.endswith('\r\n') :
            data = data[:-2]
        elif data.endswith('\n') :
            data = data[:-1]
        return data

    def cat( self, hash, path ) :
        if sys.platform == 'win32' :
            # see if cygwin's cat is available
            # if yes then use 'simulate_cat'
            # else use annotate
            cygwin_cat = r'c:\cygwin\bin\cat.exe'
            if os.path.isfile(cygwin_cat) :
                return self.simulate_cat( hash, path,
                        cat_cmd=cygwin_cat )
            annotate = self.annotate( hash, self.path )
            ann = ann2ascii.parse_annotate( StringIO.StringIO(annotate) )
            out = StringIO.StringIO()
            ann.write( out )
            return out.getvalue()
        else :
            return self.simulate_cat( hash, path, cat_cmd='cat' )

So, basically

  darcs diff --diff-command 'cat %2' --to-match 'hash 0a1b...' somefile.txt

The trick here is to force "diff" to use "cat" under the scene, which
in turn just echoes on stdout the file pointed by the "second
parameter" it received from darcs, simply ignoring the first. This
results in having the content of "somefile.txt" at the given patch.

As you can see, this is not very portable: under plain w32 there's no
way to get it but parsing the annotate output.

hth,
ciao, lele.



More information about the darcs-users mailing list