A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Jump to

Quick reference

Showing posts with label diff. Show all posts
Showing posts with label diff. Show all posts

How to apply a stash by name in git

1) In git it's easy to retrieve stashes by number like this:

git stash apply stash@{5}

But that number changes as you add more stashes to the list.

2) Instead you could perform some jiggery-pokery in bash to look up the stash by name:

git stash apply $(git stash list | grep "$NAME" | cut -d: -f1)

You can apply multiple stashes as long as they don't overlap.

3) Or you could save the commit and refer to it by tag:

git commit -m'The usual changes to aid debugging'
git tag foo
git reset --hard HEAD^
git cherry-pick -n foo

(-n is --no-commit)

This way if they overlap you can use git's merging/conflict mechanism.

(source)

Git: diff branch against master

Compare feature branch changes with upstream:

git diff $( git merge-base master HEAD )

Thanks Bill!

Show the full patch for a git merge commit

Use the undocumented -m and -p options to show the main commit ID.
Why are these not in the help!?

git show -m -p 25172c1

Good enough software

There's so much software available these days, it's difficult to find which is the best without laboriously downloading, installing, configuring and testing, which I don't often have time for. Unfortunately a large proportion of software is simply not good enough, or its features are poorly described.

Here is some "good enough" software I've found for Windows:
  • To diff directories, e.g. sync music or photos: FreeFileSync 6.3
  • To diff the content of files, e.g. source code: WinMerge
  • Windows Explorer alternative: xplorer 2
  • To encode mp3s: LAME
  • To see where your disk space is being used: Treesize
  • To remove spyware: Spybot: Search & Destroy
  • Torrents: qBittorrent
  • N64 emulator: project64
  • Programmer's text editor: Notepad++
  • ebook management: Calibre
  • Manage iPhone music outside iTunes: Media Monkey
  • Graphics maniplation: GIMP
  • Rename files: Bulk Rename Utility
  • Macro recorder: Autohotkey
  • Media player: VLC, or MPC-HC (not tried)
  • Music player: Winamp
  • Documents and spreadsheets: Microsoft Office
    • (LibreOffice and OpenOffice are NOT good enough)

Determine differences in an array with Perl

Not just checking whether or not two arrays are the same, but finding out how they are different, i.e. what has been added and what has been taken away:

use Algorithm::Diff 'sdiff';
use Data::Dumper;

my @a = (1, 2, 3, 4);
my @b = (2, 3, 4, 5);

my @c = sdiff(\@a, \@b);

print Dumper(\@c);

Output:

$VAR1 = [
          [ '-',  1, '' ],
          [ 'u',  2,  2 ],
          [ 'u',  3,  3 ],
          [ 'u',  4,  4 ],
          [ '+', '',  5 ]
        ];

You can see that 1 has a '-' indicating it has been removed, and 5 has a '+' indicating it has been added. The other items have a 'u', indicating they are unchanged.

(source)

However, if comparing (a b c) with (c b a), Algorithm::Diff::sdiff will fail to realise that nothing has been added or removed. A simpler algorithm might be:

sub determine_changes {
    my ($old, $new) = @_;

    # Compare two lists
    my %old_lookup = map { $_ => 1 } @$old;
    my %new_lookup = map { $_ => 1 } @$new;
    my @added = grep { ! $old_lookup{$_} } @$new;
    my @removed = grep { ! $new_lookup{$_} } @$old;

    my @changes;
    push @changes, map { { item => $_, operation => 'add'    } } @added;
    push @changes, map { { item => $_, operation => 'remove' } } @removed;

    my @sorted_changes = sort { $a->{item} cmp $b->{item} } @changes;
    return \@sorted_changes;
}

vimdiff fun with git

Step 1: add this to your .gitconfig
[diff]
  external = git_diff_wrapper
[pager]
  diff =


Step 2: create a file named git_diff_wrapper, put it somewhere in your $PATH
#!/bin/sh
vimdiff "$2" "$5"


Still have access to the default git diff behavior with the --no-ext-diff flag. Here’s a function to put in your bash configuration files:
function git_diff() {
  git diff --no-ext-diff -w "$@" | vim -R -
}


Source: http://technotales.wordpress.com/2009/05/17/git-diff-with-vimdiff/

Diff fun with vi and svn

Diff two files side-by-side
(search for this to highlight differences only -- replace ^I with actual tabs -- / <^I| <$| >^I| >$| \|^I )
diff --side-by-side [a] [b] | less

Diff two files side-by-side using vi
(use Ctrl-W+W to switch sides; may need :syntax off to prevent colours clashing)
vimdiff [a] [b]

Subversion diff using vi
Step 1. Create a diff command script for svn (and set the execute bit):
#!/bin/sh
# Subversion provides the paths we need as the sixth and seventh parameters.
LEFT=${6}
RIGHT=${5%    (working copy)}
[ "$RIGHT" == "$5" ] && RIGHT=$7
# Call the diff command (change the following line to make sense for your merge program).
# diffopt+=iwhite ignores whitespace differences
# \<c-w>\<c-w> switches to the right-hand pane
vimdiff "+set diffopt+=iwhite" '+execute "normal \<c-w>\<c-w>"' $LEFT $RIGHT


Step 2. Create a diff script for you to use:
#!/bin/bash
# Call the diff command (change the following line to make sense for your merge program).
# "-x -w" ignores all whitespace
svn diff -x -w --diff-cmd /path/to/diff/command/script/above $1


Step 3. Run the diff script
name_of_your_script [file in workspace]