GIT merge-svn

How to use GIT to merge two SVN branches

TL;DR: Download the git-merge-svn script here

I’ve been using git for years now but had to start using SVN for some projects. I found that GIT is good enough Subversion client too, especially as I retain the ability to commit often and rebase my work on top of commits from other devs (on the SVN side).

The only question arose – can I merge two SVN branches so that GIT log will show the merge?

The git-svn manual states that one should avoid all git clone|merge|pull|push activity when using git-svn.

But git log does show merge history that was created in Subversion – how does it do that?

svn:mergeinfo

Subversion does not support actual merge of branches (more like cherry-picking), but since version 1.5 Subversion supports the svn:mergeinfo property that is used to track what has been merged into this folder previously.

Digging some more into the matter, I found out that GIT supports setting svn:mergeinfo property on the SVN branch when dcommit‘ing:

git svn dcommit --mergeinfo "/branches/somebranch:1-3"

NB! the svn:mergeinfo is overwritten with whatever is given on the command-line, so be careful to list previous merges too.

While more recent git version added the config parameter to automatically set this property:

config key: svn.pushmergeinfo

I had some troubles with the automatic mergeinfo – for one reason or the other GIT calculated it wrong and I couldn’t get it to work.

SOLUTION: git-merge-svn

To automate the process, I wrote a shell script git merge-svn which can be used to merge two SVN branches with correct svn:mergeinfo set on the dcommit.

The script handles both situations:

  • the branch is not merged in git – will do git merge beforehand
  • the branches have been already merged in git (but not in SVN) – will traverse until previous ancestor for the merged commit revisions.

Download the git-merge-svn script here

Example usage

With this script I was able to produce these merges solely on git-side and retain the merge info so that GIT graph shows the log nicely:

git-merge-svn result

  1. Make some commits on devel6
  2. dcommit devel6 to SVN (required to get SVN revision numbers for the commits)
  3. check out testtunk6 – yes, I know I made a typo in the name ;-)
  4. git merge-svn devel6

The last commant outputs:

% git merge-svn devel6
About to do an SVN merge: devel6 -> testtunk6

* NEW MERGE COMMIT
|\
| * devel6 [7b71187] (r102)
* | testtunk6 [0682a45] (r101)
 \|
  * [273d6d6] (r100)

STEP 1: GIT merge
Executing:
  git merge devel6

Continue? (y/n) [n]: y
Merge made by the 'recursive' strategy.
 testfile | 1 +
 1 file changed, 1 insertion(+)

STEP 2: SVN dcommit

executing:
git svn dcommit --mergeinfo
/idp/branches/devel:9-32,35-41 /idp/branches/devel6:89 /idp/branches/devel6:94 /idp/branches/devel6:93 /idp/branches/devel6:96 /idp/branches/devel6:97 /idp/branches/devel6:99 /idp/branches/devel6:100 /idp/branches/devel6:102

Continue? (y/n) [n]: y
Committing to https://my.svn.host/svn/auth/idp/branches/testtunk6 ...
  M testfile
Committed r103
  M testfile
Found merge parent (svn:mergeinfo prop): 7b71187fc371d3f86658c5850009e63be88157ac
r103 = 87759323cbadd38bac78087d57b6870a926287e7 (refs/remotes/svn/testtunk6)
No changes between 3fb2168cfbbe605fbd810d76513443203a85a549 and refs/remotes/svn/testtunk6
Resetting to the latest refs/remotes/svn/testtunk6

Safari New Window

Safari New Window icon

Previously I have written AppleScripts to open new windows for TextMate, Sublime Text 2 and also Safari. But the latter was somehow corrupt when using with Mac OS X 10.8.3, so I rewrote it based on previous two, adding features in the process:

  1. Checks if Safari is already running and creates new window (without switching spaces)
  2. Activates Safari if it’s not running yet.

The full source code is here:

-- quickly check if app is running.
-- Thanks https://discussions.apple.com/message/13184433#13184433
on appIsRunning(appName)
    tell application "System Events" to (name of processes) contains appName
end appIsRunning

property appName : "Safari"

-- Open new windows or If app is not already running, start it
if appIsRunning(appName) then
    tell application appName
        make new document
        activate
    end tell
else
    tell application appName to activate
end if

Go a head and download the updated script here:

 

Download Safari New Window  

 

Rails redirect_back_or_default

In a recent project I found myself writing recirect_to :back alot, and then found myself worrying that what if for some reason there is no :back.

Drawing inspiration from this blog, I wrote two helpers in my application_controller.rb.

The store_location stores current URI (or referer URI in case of non-GET request) into session[:return_to] for later usage:

def store_location
  session[:return_to] = if request.get?
    request.request_uri
  else
    request.referer
  end
end

And redirect_back_or_default tries its best to redirect the user to somewhere, in the following order:

  1. previously stored session[:return_to]
  2. Referer URI
  3. Given default URI
  4. or root_url if all else fails

The code itself

def redirect_back_or_default(default = root_url, options)
  redirect_to(session.delete(:return_to) || request.referer || default, options)
end

I’ve found that when rewriting redirect_to :back, notice: 'something' into redirect_back_or_default-call, adding this alias helps:

alias_method :redirect_to_back_or_default, :redirect_back_or_default

But of course, if you are testing your code (and you should be), it’s better to stick to one variant of above and use tests to catch all erroneous incarnations.

Rails 3: Merge scopes

I run into a case where I had User.search method and I wanted the GroupMember model be searchable by the user’s attributes. The most DRY way to accomplish this in Rails 3 is to merge scopes. In the User model:

# user.rb
class User < ActiveRecord::Base
  has_many :memberships, :class_name => "GroupMember", :foreign_key => "user_id"

  def self.search(search)
    if search.present?
      query = []
      params = []
      %w(uid email name).each do |field|
        # The field name must be fully qualified to merge scopes
        query << "#{self.table_name}.#{field} LIKE ?"
        params << "%#{search}%"
      end
      query = query.join(" OR ")
      where(query, *params)
    else
      scoped
    end
  end
end

NB! It’s important to have the User’s field names fully qualified so that they won’t be applied to the GroupMember table. And in the GroupMember model:

# group_member.rb
class GroupMember < ActiveRecord::Base
  belongs_to :user
  belongs_to :group

  def self.search(search)
    if search.present?
      # We search GroupMembers by the user attributes
      scoped.joins(:user).merge(User.search(search))
    else
      scoped
    end
  end
end

Now it’s possible to search for GroupMembers by the User attributes:

group = Group.find 1
group.group_members.search('david')

This results in SQL query:

SELECT "group_members".* FROM "group_members" INNER JOIN "users"
ON "users"."id" = "group_members"."user_id" WHERE "group_members"."group_id" = 1
AND (users.uid LIKE '%david%' OR users.email LIKE '%david%'
OR users.name LIKE '%david%')

Sublime Text 2 New Window (via AppleScript)

Building upon my older post on opening new window in Text Mate 2, I now threw together small script to do the same in Sublime Text 2.

While Sublime Text 2 is superb text editor, it is not Mac native, thus it’s even less probable that a new Mac-style Dock icon menu will appear in near future, that allows opening new window quickly. Thus a helper application is in order.

SublNewWindow

Based on the original TMNewWindow code, only small modifications were needed. One unexpected behavior was with detecting working state of the application. For whatever reason the original script gave errors when run against Sublime Text 2.

Again, the AppleScript application is linked below and feel free to send me updates and patches if needed.

 


 

uses of Ruby’s Object#tap

Ruby 1.9′s Object#tap method has always seemed useful to me, but until now I hadn’t met the chance to use it. Every other time it seemed like abusing it in some way.

Now I came to an old code. Consider this:

cgi.text_field( "name" => "myfield",
                "value" => value,
                "size" => 20,
                "maxlength" => maxlength
              )

I needed to turn that maxlength into a conditional attribute (omiting it if it’s nil).
One way would have been extracting the attributes into a separate variable:

attrs = {
          "name" => "myfield",
          "value" => value,
          "size" => 20
        }
attrs['maxlength'] = maxlength if maxlength
cgi.text_field( attrs )

But this separates visually the cgi.text_field() call from it’s arguments, which I don’t like. Tap to the resque:

cgi.text_field({  "name" => "myfield",
                  "value" => value,
                  "size" => 20
                }.tap{|attrs| 
                  attrs['maxlength'] = maxlength if maxlength
                })

Now, isn’t that nice! (OK, maybe it isn’t, but at least it is encompassed in the method call and makes it easy to spot all attributes).

Restore DBD::MySQL::Database#quote method

For whatever reason, Ruby DBI and DBD have axed the quote method from their midst, breaking compatibility with previous versions. Sadly I couldn’t find any clues as to why it was removed (actually, bluntly commented out).

Fortunately I was able to unearth some hints how to restore this method and with an added fix to NilClass, the result seems to be working fine:

# For some unknown reason quote method is commented out at:
#   https://github.com/raphaelcosta/rails-dbd-mysql/blob/master/lib/dbd/mysql/database.rb
# 
# Restoring it for b/w compliance
module DBI::DBD::Mysql
  class Database < DBI::BaseDatabase
    def quote(value)
        case value
        when String
          "'#{@handle.quote(value)}'"
        when DBI::Binary
          "'#{@handle.quote(value.to_s)}'"
        when TrueClass
          "'1'"
        when FalseClass
          "'0'"
        when Array
          value.collect { |v| quote(v) }.join(", ")
        when DBI::Date, DBI::Time, DBI::Timestamp, ::Date
          "'#{value.to_s}'"
        when ::Time
          "'#{value.rfc2822}'"
        when NilClass
          "NULL"
        else
          value.to_s
        end
    end
  end
end

Just place this in some file and require it in your code.

 

Mount partitions like a boss (AppleScript)

How to quickly mount partitions when the need arises? A small AppleScript to help you out.

The need

No good comes without little drawbacks. When I swapped ODD with HDD, I noticed that it was quite a bit noisier than SSD and even compared to similar MBP with HDD in original bay (probably because ODD bay has a slot that lets noise out easily). So, now I tend to eject the secondary disk when I don’t need it, so my Mac can spin it down. All nice and dandy, but reattaching it when needed was a bit tedious.

The Script

set partList to {"Meedia", "BOOTCAMP"}
choose from list partList with prompt "Choose partitions" with multiple selections allowed
if result is not equal to false then
	repeat with part in result
		do shell script "diskutil mount " & part
		display dialog "Volume " & part & " mounted"
	end repeat
end if

This script asks for which of the partitions to mount and then iterates with diskutil through the selection.

Download

You can copy-paste the above script to AppleScript Editor and adjust it to your needs, or you can download this as a ready to use application.

MountPartitions.zip

 

Installing Windows 7 on MacBook Pro without SuperDrive

Right after I swapped my SuperDrive to second internal HDD I was graving to move my 20 GB Boot Camp partition to the other drive and make it bigger, so that I don’t have to carry another USB disk for my games.

UPDATE: From the comments it seems that on Mountain Lion 10.8.2 you do NEED rEFIt for the Windows partition to be visible in Boot Menu.

UPDATE 2: I have confirmed lately that the official Boot Camp Assistant method does work on MacBook Pro Retina (Mid 2012) with OS X 10.8.2 (12C3012) on it. So if your Mac came without SuperDrive from the Factory, try the official method first.

UPDATE 3: @Jorge_Rui posted excellent step-by-step instructions down in the comments on how he got it working. Take a look.

What didn’t work

You can skip to Success Story if you are not curious. Also, YMMV, so if my method does not work, you can try one of these and see if you have success with them.

USB-booting installer

Boot Camp Assistant warning that I need optical drive

First I tried to fake Boot Camp Assistant to create bootable USB stick, but that did not boot for some reason. Also, booting from USB-DVD did not work. Then I used Virtual Box to fully install Windows on physical partition and that did not show up in the boot menu either. With all of these options I also combined rEFIt to no avail.

EFI, rEFIt and File Vault 2

Intel Macs have been using EFI instead of BIOS for booting up the system since the beginning. If Boot Camp dual-booting is not enough for you, there is the rEFIt alternative boot manager that gives you more power over boot options. But it turns out that although rEFIt installs without any complaints, it fails to load from File Vault 2 encrypted partition, which is understandable as I haven’t yet provided my passkey.

So, running out of options, I decided to decrypt my partition, which I’d have had to do anyway sometime to be able to resize the encrypted partition over the previous Boot Camp partition (Disk Utility is not able to resize encrypted partitions). I still had rEFIt installed and I retried some of previous failed attempts, including booting from USB, but still no effect. Finally I noticed that I had actually two boot loaders – Mac’s own Option-key triggered menu and then the rEFIt that was installed on the primary Mac OS X partition. While most of the time Mac menu didn’t show me anything besides primary partition and Recovery HD, rEFIt showed me Windows partition (sometimes two of them pointing to the same partition), but was not able to boot from them (giving different errors from EFI failures to Windows complaining that winload.exe is missing or corrupt). In the end I removed rEFIt altogether.

The Success Story

OK, enough of the failures. What ended up working was a variant of the Virtual Machine method, that used Virtual Box to make the partition bootable and then copy over the installation files to that partition. Unfortunately I can not find the original post that lead me to the idea, but it was probably somewhere in this thread.

Note: At this point I had tried multitude of setups already and I can’t be sure that all of the steps below are necessary nor that all of the required steps are listed. If you find some errors, please comment on them.

Create partition

Create a partition in some way. You can use Boot Camp Assistant to shrink existing HFS+ partition and create a FAT32 partition or you can do it yourself via Disk Utility or diskutil command line tool. I had my partition left over from one of the tries with Boot Camp Assistant and USB DVD-drive. Using Boot Camp for this step has the side effect that it gives you the option to download latest Boot Camp drivers for windows (just have a USB stick ready to store them).

Let Boot Camp Assistant download latest support software

Set up Virtual Box guest

Now eject your Boot Camp partition so that it can be remounted elewhere. (Thanks, Bill, for pointing out that I had omitted this step). I used Oracle’s excellent (and free) Virtual Box virtualization tool. To get Virtual Box to use your physical Boot Camp partition, you have to make a raw disk image that is bound to your physical disk. In my case it was the disk1 and I partition number 3 (disk1s3 as seen from Disk Utility’s Info). To create the image, change directory to some good enough place to hold the file and enter (NB! adapt to your needs):

sudo VBoxManage internalcommands createrawvmdk -rawdisk /dev/disk1 -filename bootcamp.vmdk -partitions 3

Next, give yourself access to the physical disk and the just created image files:

sudo chmod 777 /dev/disk1s3
sudo chown $USER bootcamp*.vmdk

Last thing is to actually set up Virtual Box Guest OS. There is nothing special there, except that you specify your * bootcamp.vmdk* as the startup disk, instead of creating new one.

Installing Windows

Install Windows to the Virtual Box guest as usual. I shut down the Virtual Box client at the “Setting up Windows for first use” step, but according to some posts (links to which I again have misplaced), you could stop even at the first reboot, though it didn’t seem to work in my case.

Now you should have a partition that is visible to the Mac Boot menu, but not a working Windows installation.

Next step is to restart the install, only this time on the real hardware. To accomplish this, mount the Boot Camp partition, delete everything and copy over all files from the installer ISO (Alo commented below that on his newer Mac Mini he did not need this step, but instead installed rEFIt to boot the new partition). NB! You probably need to have some kind of NTFS driver, either NTFS-3G (see my blog on how to get NTFS-3g working in Lion) or some commercial driver like the Paragon NTFS for Mac OS X I have installed.

After you copy over the files, reboot your Mac and hold down Option-key to access the Windows partition. Now install windows as you would if you had with optical disk attached.

Conclusion

Now that I have finally managed to jump through all those hoops to get Windows installed, I can only wish that Virtualization advances enough that I could play those old Call of Duty games without even rebooting into Windows. Until then, I hope to preserve my newly installed Windows. ;-)

MacBook Pro bidrive or DIY optical bay HDD Caddy

OptiBay

If you ever run out of disk space on your laptop you might have wondered if you could replace your Optical Disc Drive which you rarely use anymore with another HDD. Turns out that of course you can and there are plenty of manufacturers out there who sell kits to do that. One of the most famous in Mac community is the MCE Technologies OptiBay pictured right.

But I live in Estonia and of course I wanted it NOW, so I went out for a little adventure to see if I can fit one of those el cheapo Akasa N.Stor HDD to ODD cases found in local PC store.

For the imatient: yes I can.

Read more »