GitHub Subtrees

I have been familiar with git ever since I started using Linux. I used ‘git clone’ quite a bit to compile and install latest versions of software, but that’s where most of my ‘familiarity’ was limited to. I did mess around with creating local repositories and committing but that’s all. We used SVN at work, so most of my version control knowledge is based on SVN. So, I am still figuring out lot of git stuff that most people probably already know.

So, I was looking for a way to have a single repository for a class and all my projects for that class would be stored under that repository. That’s when I came across this nifty little feature here called Git subtree merges, which allows you to manage multiple projects under a single repository.

The workflow as I understand it is as below:

  1. I created a repository called advanced-data-structures. This is my main repository.
  2. Then I created another repository called RedBlackEventCounter. This holds the actual project code. Made changes, committed and pushed to this remote.
  3. Then I added the remote of RedBlackEventCounter to the master of advanced-data-structures.

    git remote add -f RedBlackEventCounter https://github.com/sayakbiswas/RedBlackEventCounter.git

  4. Merged RedBlackEventCounter into advanced-data-structures.

    git merge -s ours --no-commit RedBlackEventCounter/master

  5. Copied the git data of RedBlackEventCounter repository into a new directory in the advanced-data-structures repository.

    git read-tree --prefix=RedBlackEventCounter/ -u RedBlackEventCounter/master

  6. Commit and push the changes.

    git commit -m "Added RedBlackEventCounter reference into advanced-data-structures"
    git push origin master

Now, this subtree doesn’t automatically sync with the changes made in the upstream repository. GitHub’s documentation recommends using the below command to update:

git pull -s subtree remotename branchname

[Update – 03/15/2016]: The above command seems to work properly now. I’m not sure what had happened earlier; probably some mistake on my part. Need to dig deeper. So my workflow is now:

git pull -s subtree RedBlackEventCounter master

git push origin master

The below is not needed anymore.

But for some reason, it doesn’t work out for me. I keep getting the error

error: Entry %filename% overlaps with %filename%. Cannot bind.

So, what I do is create a temporary directory and pull in everything from subtree remote. Copy over everything from this directory to the subtree directory. Remove the temporary directory, stage all, commit and push.

mkdir RedBlackTemp
git read-tree --prefix=RedBlackTemp/ -u RedBlackEventCounter/master
git commit -m "Pulled upstream changes"
cp -rf RedBlackTemp/* RedBlackEventCounter/
git rm -rf RedBlackTemp
git add .
git commit -m "Merged upstream changes into master"
git push origin master

I know it is quite cumbersome to do this everytime you make a change. I guess there is a better way that I just don’t know yet. So, if anybody has an idea, please let me know.

Playing MP3 files with java…

For a language as popular as Java, lack of standard ways of implementing mp3 playback is disappointing. This is understandable though, due to horde of patent and licensing issues relating to MP3 as a format. A bit of googling and I found two methods: using the Java Sound API along with the MP3 plugin and using the third party JLayer API from JavaZoom. Here I’m using JLayer as it seems to be the more popular alternative.

First we need to download the jlayer from here. Extract it and add the jl1.0.1.jar to the project. When using an IDE like eclipse adding the jar to the build path suffices otherwise  jl1.0.1.jar should be added to the CLASSPATH.

JLayer provides two built in players that play mp3s using the library: javazoom.jl.player and javazoom.jl.player.advanced. I’m using the Player class.

Create an instance of the Player class while providing a FileInputStream object of the mp3 file. Then use the play() method of the class to start playback.

Below is a very basic implementation of the playback process:

import java.io.*;
import javazoom.jl.player.Player;

public class JavaPlayerTest {

public static void main(String[] args) {
try
{
FileInputStream mp3file = new FileInputStream(args[0]);
Player playmp3 = new Player(mp3file);
playmp3.play();
}catch (Exception e) {
System.out.println(e);
}
}
}

Set up C/C++ environment with vim & mingw

For those who love writing code in vi/vim(let’s face it its the greatest code editor in the world) and miss it while using Windows, it is possible to set up vim in Windows. Here are the steps to install and set up Vim with MinGW(gcc/g++).

1. Download and install gVim from here.

2. Download MinGW automated installer from here and run the installer.

3. Select the package repository and the directory to install(default is C:\MinGW, I recommend leaving it as it is).

4. Select the optional components like C++, Fortran and MSYS base(this is useful; it provides the various bash shell tools like ls,grep,make,gawk etc).

5. The installer then downloads the latest version of the packages and installs them.

6. Finally, add MinGW’s bin directory to the PATH environment variable.

7. To test if MinGW is set up correctly, go to the command prompt and type g++ --version. If you see the version number as output, then congratulations.

Hope, this post has been helpful.