Monday, December 30, 2013

My Git bits and patterns

Based on the excellent guide by Sam Livingston-Gray: http://think-like-a-git.net

1. Scout Pattern for testing a merge.


Switch to a branch to merge into:
$ git checkout feature_A

        I---J---K feature_B
       /
      F---G---H feature_A
     /
A---B---C---D---E master

Create and checkout a new branch:
$ git checkout -b test_merge

          I---J---K feature_B
         /
        /       L test_merge
       /       /
      F---G---H feature_A
     /
A---B---C---D---E master

Merge with the other branch:
$ git merge feature_B

          I---J---K feature_B
         /         \
        /       L---M test_merge
       /       /
      F---G---H feature_A
     /
A---B---C---D---E master

Resolve merge conflicts and test the merge.

If merge works, keep it and merge into the feature_A:
$ git checkout feature_A
$ git merge test_merge

If merge does not work, switch to feature_A (or "master") and delete the test_merge:
$ git checkout feature_A
$ git branch -D test_merge

To abort the merge:
$ git reset --hard
or
$ git reset --hard feature_A


2. Relative references for testing a merge.


This is an extension of the Scout Pattern, with no extra step to create a checkpoint.

Switch to a branch to merge into:
$ git checkout feature_A

        I---J---K feature_B
       /
      F---G---H feature_A
     /
A---B---C---D---E master

Merge with the other branch:
$ git merge feature_B

        I---J---K feature_B
       /         \
      F---G---H---L feature_A
     /
A---B---C---D---E master

Resolve merge conflicts and test the merge.

If merge works, just keep the feature_A, and do nothing.
If merge does not work, hard reset to a prior checkpoint:
$ git reset --hard HEAD^

        I---J---K feature_B
       /
      F---G---H feature_A
     /
A---B---C---D---E master

Note: the relative checkpoint syntax HEAD^, HEAD^^, HEAD~3, etc.

To abort the merge:
$ git reset --hard


3. Undo a commit with missing info, misspelled messages, etc.


Move to prior checkpoint. This leaves the working tree as it was before "reset" (all local changes since HEAD^ are preserved):
$ git reset --soft HEAD^

Edit and commit files:
$ git commit -a -c ORIG_HEAD

All of the above commands are practical and I would use them in most situations. I have yet to see a case to use the following command.


4. Rebase allows to "replay" part of checkin history at a different base.


So, having the following tree of checkins:

                I---J---K topic
               /
      F---G---H checkpoint
     /
A---B---C---D---E master

To "move" all related checkins, the following command will reapply G, H, I, J, K one-by-one, in order, at the new base "master":
$ git rebase master topic

                              I'---J'---K' topic
                             /
                  F'---G'---H' checkpoint
                 /
A---B---C---D---E master

Note: Those changes at the "topic" branch that were previously applied to "master" in some prior merges, will not be replayed by rebase. Checkins that are not part of the "topic's" history will remain unchanged (C, D, and E).

To specify a subrange of checkins to rebase from the "topic" branch, add "--onto <base>" parameter followed by a range ("checkpoint" to "topic"):
$ git rebase --onto master checkpoint topic

        F---G---H checkpoint
       /
      /           I'---J'---K' topic
     /           /
A---B---C---D---E master


Tuesday, December 3, 2013

FAR File Manager, Beyond Compare, Notepad++ etc. in OS X with Wine

Readjusting to a new platform takes time. For me the most painful departure was from FAR (File and Archive Manager), Beyond Compare (by Scooter Software) and some other tools.

Wine for Mac OS X helps mitigate transition. It wraps platform API (Darwin) into a subset of Win32 libraries, has a convenient installer by Mike Kroneberg, called WineBottler.
Wine is a much more efficient way to experience native Windows applications on a Mac without having to run a full Windows OS in a virtual machine. This saves time, resources and battery life. System resources, external Flash drives, web etc. are available to Wine applications.

I will show how to install FAR, Beyond Compare and how to use them.

1. Get WineBottler
Download it from http://winebottler.kronenberg.org. Open DMG file and drag both Wine.app and WineBottler.app to Applications.

2. Run WineBottler with FAR3 installer
Click the third tab, provide location of your Win32 application installer (or binaries if it's self contained). Here I am providing an MSI installer for FAR, the latest can be downloaded from http://www.farmanager.com (select x86 build).
There is not much else to configure. If you will want to use Notepad++ then make sure to select the "npp" from the Winetricks list (as shown).
Instead of using Win32 Notepad++ on a Mac, I'd suggest you take a look at the TextWrangler (http://www.barebones.com/products/textwrangler/). It is a free multi-document editor with contextual editing, familiar Xcode shortcuts (for example, Cmd+Opt+up-or-down to switch counterparts) etc.
Click "Install". This will walk you through installation steps. You will provide location for your FAR (or any other) app, then WineBottler will set up Wine support, you will see a FAR installer window where you select FAR options (I used defaults). After it finishes you'll find a new directory in user's "Library/Application Support/com.Far3OSX...":
This is where all binaries, Win32 emulation, and Windows-like file system structure will be found. If anything breaks during or after - simply delete the entire folder.

3. Copy and configure Beyond Compare
This is optional, but it is the goal of this post. If you already have Beyond Compare on a Windows machine - all you need is copy binaries and import configuration. Otherwise you can follow simple instructions from the last step, selecting Beyond Compare installer. WineBottler will produce another "/Users/user/Library/Application Support/com.Beyond Compare..." self-contained installation. Grab binaries from its "drive_c/Program Files".
Copy Beyond Compare files to FAR's "drive_c/Program Files". The result should look like this:
Before you will run Beyond Compare.app (or however you called it), , it will ask you to provide license. You can copy and paste any text now from your Mac to Wine-hosted application.

4. Run your FAR3OSX.app (or however you called it)
As mentioned in this thread you will need to edit a startwine file inside the FAR3OSX.app to be able to run it as a console. At the end of the file change "$WINEUSRPATH/bin/wine" to "$WINEUSRPATH/bin/wineconsole".
Run the app. To set default width and height of a console, right-click inside the window and select "Set Default". 


Now you can explore the local drives and manipulate files as you would in Windows. Note that Z: drive refers to the root of your Mac drive, so be cautious. C: drive is an alias to a directory "/Users/user/Library/Application Support/com.Far3OSX_.../drive_c".
Also note that Wine maps "Alt" key to a Command key on Mac keyboard. So instead of hitting "Alt+F1" or "Alt+F2" to change drives, use "Command+F1/F2".
FAR has lots of useful features, some of which are:

  • Built-in text/HEX viewer and editor (with color-coded keywords and familiar copy-paste).
  • Instant history for commands, folders, files. These are also used for line completion.
  • Built-in archiver with a shortcut.
  • Batch folder size calculation.
  • Powerful menu system. More on this next.

5. FAR3 User Menu
Press F2 to invoke the user-configurable menu. In this example I've configured it to list Beyond Compare and a Notepad++.
To edit a single entry hit F4, to open a menu file in an editor hit Command-F4. My menu file looks as follows. Note that I am using full paths to application executables. All the special symbols signify placeholders for the input from the two panels. To learn more about them - press F1 while in user menu.

1:  Beyond Compare 2 panels
    "C:\Program Files\Beyond Compare 2\bc2.exe" "!\" "!#!\"
2:  Beyond Compare 2 selected files
    "C:\Program Files\Beyond Compare 2\bc2.exe" !&
3:  Beyond Compare 2 files in 2 panels
    "C:\Program Files\Beyond Compare 2\bc2.exe" "!^!\!.!" "!#!\!.!"
--:  Notepad++
4:  Notepad++ [files] || [dir]
    "C:\npp\notepad++.exe" !&


The power of FAR now can be seen whenever you need to compare and sync folders or files. Just navigate left panel to one folder and the other panel to a different folder, press F2 and select to "Beyond Compare 2 panels".
Here I am syncing a folder on SanDisk Sansa MP3 player with a folder on a Flash drive.


On the first run Beyond Compare will ask you to register it, or to provide license. FAR and Notepad++ are free.

That's it. Of course there are similar tools available on Mac, but if you've purchased your Windows apps and would rather continue using them - I believe Wine is the best friend.




Sunday, November 24, 2013

C++ Refresher - Operator Precedence


Operator Precedence
(highlighted are the changes from R-value to L-value in C++ compared to C)
Operator      Description                    Result    Associativity

() Grouping exp N/A
() Function call rexp L-R [] Subscript lexp L-R . Structure member lexp L-R -> Structure pointer member lexp L-R ++ Postfix increment rexp L-R -- Postfix decrement rexp L-R
! Logical negate rexp R-L ~ One's complement rexp R-L + Unary plus rexp R-L - Unary minus rexp R-L ++ Prefix increment LEXP R-L -- Prefix decrement LEXP R-L * Indirection (dereference) lexp R-L & Address of rexp R-L sizeof Size in bytes rexp R-L
(type) Type conversion (cast) rexp R-L
* Multiplication rexp L-R / Division rexp L-R % Integer remainder (modulo) rexp L-R
+ Addition rexp L-R - Subtraction rexp L-R
<< Left shift rexp L-R >> Right shift rexp L-R
> Greater than rexp L-R >= Greater than or equal rexp L-R < Less than rexp L-R <= Less than or equal rexp L-R
== Equal to rexp L-R != Not equal to rexp L-R
& Bitwise AND rexp L-R
^ Bitwise exclusive OR rexp L-R
| Bitwise inclusive OR rexp L-R
&& Logical AND rexp L-R
|| Logical OR rexp L-R
?: Conditional LEXP N/A
= Assignment LEXP R-L += Add to LEXP R-L -= Subtract from LEXP R-L *= Multiply by LEXP R-L /= Divide by LEXP R-L %= Modulo by LEXP R-L <<= Shift left by LEXP R-L >>= Shift right by LEXP R-L &= AND with LEXP R-L ^= Exclusive OR with LEXP R-L |= Inclusive OR with LEXP R-L
, Comma rexp L-R


Common non-printing control characters

0 Null 7 Bell 8 Backspace 9 Tab 10 Line feed 13 Carriage return 26 End of file (Ctrl-Z) 27 [Esc] (Escape key) ASCII characters (only 32-127 are standard)
32 64 @ 96 ` 128 € 160   192 + 224   33 ! 65 A 97 a 129  161 ¡ 193 - 225 á 34 " 66 B 98 b 130 ‚ 162 ¢ 194 - 226 A 35 # 67 C 99 c 131 ƒ 163 £ 195 + 227 d 36 $ 68 D 100 d 132 „ 164 ¤ 196 - 228 O 37 % 69 E 101 e 133 … 165 ¥ 197 + 229 ¢ 38 & 70 F 102 f 134 † 166 ¦ 198 Ý 230 æ 39 ' 71 G 103 g 135 ‡ 167 § 199 Ý 231 “ 40 ( 72 H 104 h 136 ˆ 168 ¨ 200 + 232 ™ 41 ) 73 I 105 i 137 ‰ 169 ª 201 + 233 E 42 * 74 J 106 j 138 Š 170 ª 202 - 234 U 43 + 75 K 107 k 139 ‹ 171 « 203 - 235 „ 44 , 76 L 108 l 140 Œ 172 ¬ 204 Ý 236 8 45 - 77 M 109 m 141  173 ­ 205 - 237 ” 46 . 78 N 110 n 142 Ž 174 ® 206 + 238 † 47 / 79 O 111 o 143  175 ¯ 207 - 239 n 48 0 80 P 112 p 144  176 Ý 208 - 240 = 49 1 81 Q 113 q 145 ‘ 177 Ý 209 - 241 ñ 50 2 82 R 114 r 146 ’ 178 Ý 210 - 242 = 51 3 83 S 115 s 147 “ 179 Ý 211 + 243 = 52 4 84 T 116 t 148 ” 180 Ý 212 + 244 ( 53 5 85 U 117 u 149 • 181 Ý 213 + 245 ) 54 6 86 V 118 v 150 – 182 Ý 214 + 246 ö 55 7 87 W 119 w 151 — 183 + 215 + 247 ~ 56 8 88 X 120 x 152 ˜ 184 + 216 + 248 ø 57 9 89 Y 121 y 153 ™ 185 Ý 217 + 249 ú 58 : 90 Z 122 z 154 š 186 Ý 218 + 250 ú 59 ; 91 [ 123 { 155 › 187 + 219 Ý 251 v 60 < 92 \ 124 | 156 œ 188 + 220 _ 252 n 61 = 93 ] 125 } 157  189 + 221 Ý 253 ý 62 > 94 ^ 126 ~ 158 P 190 + 222 Ý 254 Ý 63 ? 95 _ 127 Ý 159 Ÿ 191 + 223 _ 255


%c - characters
%s - strings (NULL terminated C strings)
%d, %i - integers
%f - floating point
%g - floating point (minimum digits)
%e - scientific notation
%p - pointers (displays in hex)
%x - hexadecimal integers
%o - octal integers
%u - unsigned integers
%ld, %li - long integers
%lu - unsigned long integers
%hd, %hi - short integers
%hu - unsigned short integers

C Refresher - Operator Precedence


Operator Precedence
Operator      Description                    Result    Associativity

() Grouping exp N/A
() Function call rexp L-R [] Subscript lexp L-R . Structure member lexp L-R -> Structure pointer member lexp L-R ++ Postfix increment rexp L-R -- Postfix decrement rexp L-R
! Logical negate rexp R-L ~ One's complement rexp R-L + Unary plus rexp R-L - Unary minus rexp R-L ++ Prefix increment rexp R-L -- Prefix decrement rexp R-L * Indirection (dereference) lexp R-L & Address of rexp R-L sizeof Size in bytes rexp R-L
(type) Type conversion (cast) rexp R-L
* Multiplication rexp L-R / Division rexp L-R % Integer remainder (modulo) rexp L-R
+ Addition rexp L-R - Subtraction rexp L-R
<< Left shift rexp L-R >> Right shift rexp L-R
> Greater than rexp L-R >= Greater than or equal rexp L-R < Less than rexp L-R <= Less than or equal rexp L-R
== Equal to rexp L-R != Not equal to rexp L-R
& Bitwise AND rexp L-R
^ Bitwise exclusive OR rexp L-R
| Bitwise inclusive OR rexp L-R
&& Logical AND rexp L-R
|| Logical OR rexp L-R
?: Conditional rexp N/A
= Assignment rexp R-L += Add to rexp R-L -= Subtract from rexp R-L *= Multiply by rexp R-L /= Divide by rexp R-L %= Modulo by rexp R-L <<= Shift left by rexp R-L >>= Shift right by rexp R-L &= AND with rexp R-L ^= Exclusive OR with rexp R-L |= Inclusive OR with rexp R-L
, Comma rexp L-R

ASCII Characters

Common non-printing control characters

0 Null 7 Bell 8 Backspace 9 Tab 10 Line feed 13 Carriage return 26 End of file (Ctrl-Z) 27 [Esc] (Escape key) ASCII characters (only 32-127 are standard)
32 64 @ 96 ` 128 € 160   192 + 224   33 ! 65 A 97 a 129  161 ¡ 193 - 225 á 34 " 66 B 98 b 130 ‚ 162 ¢ 194 - 226 A 35 # 67 C 99 c 131 ƒ 163 £ 195 + 227 d 36 $ 68 D 100 d 132 „ 164 ¤ 196 - 228 O 37 % 69 E 101 e 133 … 165 ¥ 197 + 229 ¢ 38 & 70 F 102 f 134 † 166 ¦ 198 Ý 230 æ 39 ' 71 G 103 g 135 ‡ 167 § 199 Ý 231 “ 40 ( 72 H 104 h 136 ˆ 168 ¨ 200 + 232 ™ 41 ) 73 I 105 i 137 ‰ 169 ª 201 + 233 E 42 * 74 J 106 j 138 Š 170 ª 202 - 234 U 43 + 75 K 107 k 139 ‹ 171 « 203 - 235 „ 44 , 76 L 108 l 140 Œ 172 ¬ 204 Ý 236 8 45 - 77 M 109 m 141  173 ­ 205 - 237 ” 46 . 78 N 110 n 142 Ž 174 ® 206 + 238 † 47 / 79 O 111 o 143  175 ¯ 207 - 239 n 48 0 80 P 112 p 144  176 Ý 208 - 240 = 49 1 81 Q 113 q 145 ‘ 177 Ý 209 - 241 ñ 50 2 82 R 114 r 146 ’ 178 Ý 210 - 242 = 51 3 83 S 115 s 147 “ 179 Ý 211 + 243 = 52 4 84 T 116 t 148 ” 180 Ý 212 + 244 ( 53 5 85 U 117 u 149 • 181 Ý 213 + 245 ) 54 6 86 V 118 v 150 – 182 Ý 214 + 246 ö 55 7 87 W 119 w 151 — 183 + 215 + 247 ~ 56 8 88 X 120 x 152 ˜ 184 + 216 + 248 ø 57 9 89 Y 121 y 153 ™ 185 Ý 217 + 249 ú 58 : 90 Z 122 z 154 š 186 Ý 218 + 250 ú 59 ; 91 [ 123 { 155 › 187 + 219 Ý 251 v 60 < 92 \ 124 | 156 œ 188 + 220 _ 252 n 61 = 93 ] 125 } 157  189 + 221 Ý 253 ý 62 > 94 ^ 126 ~ 158 P 190 + 222 Ý 254 Ý 63 ? 95 _ 127 Ý 159 Ÿ 191 + 223 _ 255

Common printf formatting codes

%c - characters
%s - strings (NULL terminated C strings)
%d, %i - integers
%f - floating point
%g - floating point (minimum digits)
%e - scientific notation
%p - pointers (displays in hex)
%x - hexadecimal integers
%o - octal integers
%u - unsigned integers
%ld, %li - long integers
%lu - unsigned long integers
%hd, %hi - short integers
%hu - unsigned short integers

Thursday, October 3, 2013

Prettify

Code highlight in a blogger post is not enabled by default. Luckily it is very easy to fix.


  1. Head to http://code.google.com/p/google-code-prettify/, click on README link.
  2. Open blog template and click "Edit HTML". This will open HTML editor.
  3. Find a closing tag "</head>". Insert the following lines immediately in front of it.

<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>
<link href='http://google-code-prettify.googlecode.com/svn/trunk/styles/sunburst.css' rel='stylesheet' type='text/css'/>

The first line enables Google's code highlighter Prettify. The second line is an example of how to enable a custom skin.
For Prettify to work, the code in a blog post must be delimited with "<pre class="prettyprint">...</pre>" tags. To do this - compose code samples in "HTML" mode after editing a blog post.

Examples of code highlight:

void arrays2()
{
    int c[3][7];
    int **p = c;
    
    int v = *(*(p + 3));
    
    double points[3][4] = {
        {1.0,  2.0,  3.0,  4.0},
        {5.0,  6.0,  7.0,  8.0},
        {9.0, 10.0, 11.0, 12.0}
    };

    double points2[][] = {
        {1.0,  2.0,  3.0,  4.0},
        {5.0,  6.0,  7.0,  8.0},
        {9.0, 10.0, 11.0, 12.0}
    };

    printf("points2[1][1] = %f");
}

- (id) init
{
    return [self initWithSomething:@"something"];
}

- (id) initWithSomething: (NSString *) aSomething
{
    if(self = [super init]) {
        _something = [aSomething copy];
    }
    return self;
}