Working with others in git

Series: Why git?, Basics, Branches, Merging, Remotes

Now I’ve covered almost everything you need to understand the basics of how git works. In this video I discuss how to clone, pull from and fork remote git repositories, and how to send patches and make pull requests to a project maintainer.

Slides: Working with others in git.

Merging in git

Series: Why git?, Basics, Branches, Merging, Remotes

The last video covered how to make branches and simple merges.

This time we look at what to do when you want just one change from another branch (cherry-picking) and how to merge two branches when there are conflicts between them. We also look at rebasing, which is a different way to combine the work done on two branches, which hides complexity from other people at the expense of changing history.

Slides: Merging in git.

Behaviour of Java String.split() when some answers are the empty string

Can you guess the output of this program?

class SplitTest
{
    static void split( String s )
    {
        System.out.println( s.split( ";" ).length );
    }

    public static void main( String[] args )
    {
        split("");
        split(";");
        split("x;");
        split(";y");
    }
}

Here it is:

$ javac SplitTest.java && java SplitTest
1
0
1
2

Wow. Docs: String.split.

Side note: It’s not easy to fit Java examples into tweets.

Checking the case of a filename on Windows

Windows generally uses a case-insensitive but not case-preserving file system.

When writing some code that is intended to be used on Linux as well as Windows, I wanted it to fail on Windows in the same cases that it would fail on Linux, and this meant detecting when the case of a filename differed from its canonical case on the file system.

I want to ask “is this file name correct in terms of case?”

I was working in Java, but I think this issue would be similar in other languages: it’s difficult to ask for the canonical case version of a file name when we currently have a filename with abitrary case.

The only solution I came up with was to list the contents of the parent directory and check whether my arbitrary filename is listed with the correct case in the results:

// CaseCheck.java

import java.util.Arrays;
import java.io.File;
import java.io.IOException;

class CaseCheck
{
    private static File parentFile( File f )
    {
        File ret = f.getParentFile();
        if ( ret == null )
        {
            ret = new File( "." );
        }
        return ret;
    }

    private static boolean existsAndCaseCorrect( String fileName )
    {
        File f = new File( fileName );
        return Arrays.asList( parentFile( f ).list() ).contains( f.getName() );
    }

    public static void main( String[] args ) throws IOException
    {
        System.out.println( existsAndCaseCorrect( args[0] ) );
    }
}

Checking it on its own source file:

javac CaseCheck.java && java CaseCheck cASEcheck.java
false

javac CaseCheck.java && java CaseCheck CaseCheck.java
true

It seems to work.

Note that this also returns false if the file doesn’t exist, and will throw an error if the file name specifies a parent directory that doesn’t exist.