Two days ago, Bob Lee posted a fun challenge on the Luhny Bin. It was not before a day has passed that I could get to know of the problem and try my hands at it. The delay being attributed to absence of Cygwin on my Windows machine which was needed to run the test harness (refer original post on details).

Yesterday, I spent most of my time first trying to get the harness running without using Cygwin. The solution was a bit tricky as I had to figure out how the test harness was actually running. Anyhow, my solution is available at here wherein I modified the original Main.java file. The change was simple: first, change mask.sh on line 41 to say mask.bat and second, modify line 81 to correctly build the process.

Once, I had it setup and after loosing all day, I was ready to start coding the actual solution. Before I dozed off to my bed, I had made sure that my code passed 18 of the 20 tests. The 19th test was tricky. It had many over-lapping luhn numbers, and hence I had some leftover in the end which was not being cleaned up. This single test made me completely rewrite the piece introducing a third buffer (which came down to two, as I directly now wrote to System.out).

Anyways, the solution is now complete and posted on my fork of the LuhnyBin Github repository.

written by Sandeep Gupta

Thursday, November 17, 2011 at 7:42 PM

While working today with EGit for the GitHub API, I found out that there is no way to get the Commit statistics and the various file details. If you ponder over the API details at http://developer.github.com/v3/repos/commits/,

GET /repos/:user/:repo/commits/:sha

the response for the API contains details about the lines added/deleted in the commit. The response also contains details about the various files that are impacted. See the *stats* and the *files* section below:

{
  "url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e",
  "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
  "commit": {
    "url": "https://api.github.com/repos/octocat/Hello-World/git/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e",
    "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
    "author": {
      "name": "Monalisa Octocat",
      "email": "support@github.com",
      "date": "2011-04-14T16:00:49Z"
    },
    "committer": {
      "name": "Monalisa Octocat",
      "email": "support@github.com",
      "date": "2011-04-14T16:00:49Z"
    },
    "message": "Fix all the bugs",
    "tree": {
      "url": "https://api.github.com/repos/octocat/Hello-World/tree/6dcb09b5b57875f334f61aebed695e2e4193db5e",
      "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e"
    }
  },
  "author": {
    "login": "octocat",
    "id": 1,
    "avatar_url": "https://github.com/images/error/octocat_happy.gif",
    "url": "https://api.github.com/users/octocat"
  },
  "committer": {
    "login": "octocat",
    "id": 1,
    "avatar_url": "https://github.com/images/error/octocat_happy.gif",
    "url": "https://api.github.com/users/octocat"
  },
  "parents": [
    {
      "url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e",
      "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e"
    }
  ],
  "stats": {
    "additions": 104,
    "deletions": 4,
    "total": 108
  },
  "files": [
    {
      "filename": "file1.txt",
      "additions": 10,
      "deletions": 2,
      "total": 12
    }
  ]
}

As I needed these values, I happened to go over the source code for *EGit* and find a way around. Must say, the way the source code is structured makes it super-easy to modify the code for additional API values.

Below are the two simple changes I made to make sure the API returns the details I needed,

Add the following class org.eclipse.egit.github.core.Stats
package org.eclipse.egit.github.core;

public class Stats {
 
 private int additions;
 
 private int deletions;
 
 private int total;

 public int getAdditions() {
  return additions;
 }

 public void setAdditions(int additions) {
  this.additions = additions;
 }

 public int getDeletions() {
  return deletions;
 }

 public void setDeletions(int deletions) {
  this.deletions = deletions;
 }

 public int getTotal() {
  return total;
 }

 public void setTotal(int total) {
  this.total = total;
 }

}

Modify the class org.eclipse.egit.github.core.RepositoryCommit to add the following lines:
private Stats stats;
 
 private List<CommitFile> files;

 public Stats getStats() {
  return stats;
 }

 public void setStats(Stats stats) {
  this.stats = stats;
 }

 public List<CommitFile> getFiles() {
  return files;
 }

 public void setFiles(List<CommitFile> files) {
  this.files = files;
 }

And we are done. Calling the following API,
public void test() {

    CommitService commitService = new CommitService();

    RepositoryCommit commit = commitService.getCommit(repository, sha);

    System.out.println("Total lines impacted: " + commit.getTotal());

    System.out.println("Total files impacted: " + commit.getFiles().size());
}

should return back the various statistics as returned by *GitHub*.

Hope this helps!

Update: Added the files as a GitHub gist at https://gist.github.com/1355351

written by Sandeep Gupta

Thursday, November 10, 2011 at 9:48 PM