<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="4.4.1">Jekyll</generator><link href="/feed.xml" rel="self" type="application/atom+xml" /><link href="/" rel="alternate" type="text/html" /><updated>2026-07-30T17:07:09-07:00</updated><id>/feed.xml</id><title type="html">Todd Lucas</title><subtitle>Software developer, etc.</subtitle><entry><title type="html">Git topic sub-branch rebasing</title><link href="/2019/12/git-topic-sub-branch-rebasing/" rel="alternate" type="text/html" title="Git topic sub-branch rebasing" /><published>2019-12-22T00:00:00-08:00</published><updated>2019-12-22T00:00:00-08:00</updated><id>/2019/12/git-topic-sub-branch-rebasing</id><content type="html" xml:base="/2019/12/git-topic-sub-branch-rebasing/"><![CDATA[<p>A feature or topic branch based workflow is an essential part of modern software development.
A topic branch gives a developer an isolated place to do work.
And once that work is done, it provides a basis for creating a pull request.</p>

<p>Once a feature is completed and pushed, sometimes it makes sense to create a second topic branch, based on the first.
These are often called sub branches.
This might happen for several reasons, including:</p>
<ul>
  <li>You’re not ready to merge the first branch</li>
  <li>You’re holding off merging, to keep the new feature out of <code class="language-plaintext highlighter-rouge">master</code>, for reasons</li>
  <li>PRs might take a while to get approved</li>
</ul>

<p>You probably know that creating one topic branch off of another is easy.
Merging this work back to master can also be pretty straightforward.
Rebasing, however, takes a little more care.</p>

<p class="note note-primary">
TL;DR: Use <code class="highlighter-rouge">git rebase --onto</code> to transplant your topic branches in the order they were created, but take care if your branch is used in a pull request.
</p>

<p>The following examples are based on this hypothetical set of topic branches.
Each consecutive topic branch a sub-branch of the earlier branch.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>    A---B---C           master
      \
       D                branch-one
        \
         E---F---G      branch-two
              \
               H---I    branch-three
</code></pre></div></div>

<p>I order, <code class="language-plaintext highlighter-rouge">branch-one</code> has a single commit, <code class="language-plaintext highlighter-rouge">branch-two</code> has three commits, and <code class="language-plaintext highlighter-rouge">branch-three</code> has two commits—but it starts halfway along the <code class="language-plaintext highlighter-rouge">branch-two</code> work.
Meanwhile, two other commits have occurred on <code class="language-plaintext highlighter-rouge">master</code>.</p>

<h3 id="merging-strategies">Merging strategies</h3>

<p>If you’re not rebasing, these branches can be easily merged to <code class="language-plaintext highlighter-rouge">master</code>, in order, with one  merge commit each.
The merge-commit approach would look like this.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>    A---B---C---D'---G'--I'   master
      \        /    /   /
       D------'    /   /
        \         /   /
         E---F---G   /
              \     /
               H---I
</code></pre></div></div>

<p>One way to merge these branches with rebasing is to build them up gradually, starting with rebasing <code class="language-plaintext highlighter-rouge">branch-three</code> onto <code class="language-plaintext highlighter-rouge">branch-two</code>, and so on.
This requires rewriting each sub-branch multiple times.
As a consequence, getting PRs to track is difficult.</p>

<p>In this post, we take the approach of rebasing onto <code class="language-plaintext highlighter-rouge">master</code>, in the order the branches were created.</p>

<h3 id="merging-branch-one">Merging branch one</h3>

<p>The first case is easy, since it’s branched directly off of <code class="language-plaintext highlighter-rouge">master</code>. You can use  standard rebase.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git checkout branch-one
git rebase master
</code></pre></div></div>

<p>We get the expected result, with a new commit, <code class="language-plaintext highlighter-rouge">D'</code>, containing the changes from commit <code class="language-plaintext highlighter-rouge">D</code> replayed on master.
The branch reference was also moved as expected, leaving the original commit, <code class="language-plaintext highlighter-rouge">D</code> without one.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>            +-------------- master
            |   
            v 
    A---B---C---D' &lt;------- branch-one      
         \
          D
           \
            E---F---G       branch-two
                 \
                  H---I     branch-three
</code></pre></div></div>

<p class="note note-warning">
If you use GitHub, and you had PRs outstanding for these branches, at this point, GitHub might become confused.
It would see that the merge target of <code class="highlighter-rouge">branch-two</code> had changed and it would close the PR associated with <code class="highlighter-rouge">branch-two</code>.
</p>

<p>To avoid this, you can edit the PR and set the target to be <code class="language-plaintext highlighter-rouge">master</code> instead of <code class="language-plaintext highlighter-rouge">branch-one</code> <em>before rebasing</em> <code class="language-plaintext highlighter-rouge">branch-one</code>.
This might cause your PR changes to look a little crazy in the interim, but they return to normal afterwards.</p>

<p>After rebasing <code class="language-plaintext highlighter-rouge">branch-one</code>, be sure to update the PR to reflect the final state of the branch.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git checkout branch-one
git push -f
</code></pre></div></div>

<p>Next, merge <code class="language-plaintext highlighter-rouge">master</code> and push so the remote repo is up to date.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git checkout master
git merge branch-one
git push
</code></pre></div></div>

<p>This will be a <em>fast-forward</em> merge if your local <code class="language-plaintext highlighter-rouge">master</code> branch was up to date prior to rebasing.</p>

<p>Finally, you’ll want to perform the normal PR cleanup:</p>
<ul>
  <li><code class="language-plaintext highlighter-rouge">git push origin -d branch-one</code> # to delete the remote branch</li>
  <li><code class="language-plaintext highlighter-rouge">git branch -d branch-one</code> # to delete your local branch</li>
</ul>

<h3 id="merging-branch-two">Merging branch two</h3>

<p>Before we get started, we want to make sure <code class="language-plaintext highlighter-rouge">branch-three</code>’s PR isn’t clobbered when we rebase.
Again, if we’re using GitHub, we will edit the PR for <code class="language-plaintext highlighter-rouge">branch-three</code> and change the merge target from <code class="language-plaintext highlighter-rouge">branch-two</code> to <code class="language-plaintext highlighter-rouge">master</code>.</p>

<p>Here’s where things change from the normal rebase flow.
We want to rebase <code class="language-plaintext highlighter-rouge">branch-two</code> onto <code class="language-plaintext highlighter-rouge">master</code>, per normal.
If we do a normal rebase, git will look all the way back to master, which includes the moribund commit, <code class="language-plaintext highlighter-rouge">D</code>.
In simple cases, this won’t be a problem, as the replay will be a no-op.
It can cause conflicts, though, if changes have been made in the interim on <code class="language-plaintext highlighter-rouge">master</code>.
Fortunately, rebase provides the <code class="language-plaintext highlighter-rouge">--onto</code> flag for exactly this situation.</p>

<p>We need to tell git to rebase only the commits from the topic branch that we care about—that is, commits <code class="language-plaintext highlighter-rouge">E</code>, <code class="language-plaintext highlighter-rouge">F</code>, and <code class="language-plaintext highlighter-rouge">G</code>.
If we wanted to move <code class="language-plaintext highlighter-rouge">branch-two</code> earlier, while <code class="language-plaintext highlighter-rouge">branch-one</code> was still in its original place, we could have run.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git rebase --onto master branch-one branch-two
</code></pre></div></div>

<p>Indeed, that’s <a href="https://git-scm.com/book/en/v2/Git-Branching-Rebasing#rbdiag_e">the canonical case</a>.
We want to rebase <code class="language-plaintext highlighter-rouge">branch-two</code>, starting from the <em>upstream</em> branch, <code class="language-plaintext highlighter-rouge">branch-one</code>, <em>onto</em> <code class="language-plaintext highlighter-rouge">master</code>.
Unfortunately, <code class="language-plaintext highlighter-rouge">branch-one</code> is no longer around.
So we need to get the hash for commit <code class="language-plaintext highlighter-rouge">D</code> as a substitute for <code class="language-plaintext highlighter-rouge">branch-one</code> for the <em>upstream</em> argument.</p>

<p>You can get it by checking out <code class="language-plaintext highlighter-rouge">branch-two</code> and running <code class="language-plaintext highlighter-rouge">git log</code> to find the commit just before <code class="language-plaintext highlighter-rouge">E</code>.
If you plan ahead, you can run <code class="language-plaintext highlighter-rouge">git log --oneline</code> from each branch to get a list of all of the relevant branch refs and their commit hashes prior to starting.</p>

<p>Log from <code class="language-plaintext highlighter-rouge">branch-three</code></p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>e61e988 (HEAD -&gt; branch-three) I
37dcec4 H
61c2db4 F
32c9e2c E
2faee42 (branch-one) D
1e10333 B
0e58a1e A
</code></pre></div></div>

<p>Log from <code class="language-plaintext highlighter-rouge">branch-two</code></p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>5f45eae (HEAD -&gt; branch-two) G
61c2db4 F
32c9e2c E
2faee42 (branch-one) D
1e10333 B
0e58a1e A
</code></pre></div></div>

<p>Once we have the hash for <code class="language-plaintext highlighter-rouge">D</code>, we can rebase <code class="language-plaintext highlighter-rouge">branch-two</code> onto <code class="language-plaintext highlighter-rouge">master</code>.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git rebase --onto master 2faee42 branch-two
</code></pre></div></div>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>                +------------------ master
                |   
                v 
    A---B---C---D'--E'--F'--G' &lt;--- branch-two      
         \
          D
           \
            E---F---G
                 \
                  H---I             branch-three
</code></pre></div></div>

<p>Now, perform the remaining steps to wrap up <code class="language-plaintext highlighter-rouge">branch-two</code>.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code># Update the PR
git checkout branch-two
git push -f

# Fast-forward merge
git checkout master
git merge branch-two
git push

# Delete the branch
git push origin -d branch-two
git branch -d branch-two
</code></pre></div></div>

<h4 id="interlude-merging-branch-two-after-squashing">Interlude: merging branch two after squashing</h4>

<p>Some teams routinely squash commits prior to merging or rebasing.
This can make it easier to see where some work ends and other work begins, especially if rebasing onto master.</p>

<p>To squash, just use interactive rebase as you normally would.
In this case, we would like to squash commits <code class="language-plaintext highlighter-rouge">E</code>, <code class="language-plaintext highlighter-rouge">F</code>, and <code class="language-plaintext highlighter-rouge">G</code> into one, say <code class="language-plaintext highlighter-rouge">E'</code>.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git checkout branch-two
git rebase -i HEAD~3
</code></pre></div></div>

<p>In the invoked editor, we would squash (or fixup) two commits.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>pick 5cc6ba4cd E
squash eda9daade F
squash c4f181c1b G
</code></pre></div></div>

<p>This would leave us with a tree that looks like this:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>    A---B---C---D' &lt;------- branch-one      
      \
       D
       |\
       | E'                 branch-two
       \
        E---F---G
             \
              H---I         branch-three
</code></pre></div></div>

<p>The rebase would then continue as before, with the <code class="language-plaintext highlighter-rouge">--onto</code> flag.</p>

<h3 id="merging-branch-three">Merging branch three</h3>

<p>The process continues for <code class="language-plaintext highlighter-rouge">branch-three</code> as it was performed for <code class="language-plaintext highlighter-rouge">branch-two</code>.
Note that in this case, <code class="language-plaintext highlighter-rouge">branch-three</code> was created partway through the work on <code class="language-plaintext highlighter-rouge">branch-two</code>.
This doesn’t create any special considerations.
It just means that you will need to select a hash for commit <code class="language-plaintext highlighter-rouge">F</code> or <code class="language-plaintext highlighter-rouge">G</code> rather than <code class="language-plaintext highlighter-rouge">E</code> for the rebase. When running rebase with branch names, git looks at the common ancestor of each branch to determine where they diverge. The same goes for commit hashes, so the choice of <code class="language-plaintext highlighter-rouge">F</code> over <code class="language-plaintext highlighter-rouge">G</code> isn’t important.</p>

<p>The final state will look like this.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>    A---B---C---D'--E'--G'--H'--I'  master
      \
       D
        \
         E---F---G
              \
               H---I
</code></pre></div></div>

<p>Now that there are no references to the original commits, they will eventually be garbage collected.</p>

<h3 id="summary">Summary</h3>

<p>Prior to starting</p>

<ul>
  <li>Check out each branch</li>
  <li>Run <code class="language-plaintext highlighter-rouge">git log --oneline</code> to get hashes associated with original branch names</li>
</ul>

<p>For each branch</p>

<ul>
  <li>Prepare relevant PRs (if using GitHub)
    <ul>
      <li>Update the merge target of the <em>descendent</em> PR to refer to <code class="language-plaintext highlighter-rouge">master</code></li>
    </ul>
  </li>
  <li>Squash (optional)
    <ul>
      <li><code class="language-plaintext highlighter-rouge">git rebase -i &lt;upstream&gt; &lt;branch&gt;</code></li>
    </ul>
  </li>
  <li>Rebase onto <code class="language-plaintext highlighter-rouge">master</code>
    <ul>
      <li><code class="language-plaintext highlighter-rouge">git rebase --onto &lt;newbase&gt; &lt;upstream&gt; &lt;branch&gt;</code></li>
    </ul>
  </li>
  <li>Update the PR
    <ul>
      <li><code class="language-plaintext highlighter-rouge">git checkout &lt;branch&gt;</code></li>
      <li><code class="language-plaintext highlighter-rouge">git push -f</code></li>
    </ul>
  </li>
  <li>Merge the branch to <code class="language-plaintext highlighter-rouge">master</code>
    <ul>
      <li><code class="language-plaintext highlighter-rouge">git checkout master</code></li>
      <li><code class="language-plaintext highlighter-rouge">git merge &lt;branch&gt;</code></li>
      <li><code class="language-plaintext highlighter-rouge">git push</code></li>
    </ul>
  </li>
  <li>Delete the branch
    <ul>
      <li><code class="language-plaintext highlighter-rouge">git push origin -d &lt;branch&gt;</code></li>
      <li><code class="language-plaintext highlighter-rouge">git branch -d &lt;branch&gt;</code></li>
    </ul>
  </li>
</ul>

<h3 id="an-aside">An aside</h3>

<p>When newcomers first learn about rebasing, it can seem confusing.
Part of this confusion arises, in my opinion, because rebasing is used to do different things—much like merge.
(This relates to <a href="https://stevebennett.me/2012/02/24/10-things-i-hate-about-git/">complaints</a> <a href="https://gist.github.com/incompl/3819571">sometimes</a> <a href="https://spderosso.github.io/onward13.pdf">heard</a> about <a href="https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain">git’s porcelain</a>.)
In this post, I referenced two different modes for using rebase.
The first (and the point of the article), is rebasing to <em>move</em> commits rather than using merge.
The second mode is rebasing to <em>combine</em> commits, often called squashing.
This is usually done using <em>interactive rebase</em>, with the <code class="language-plaintext highlighter-rouge">-i</code> or <code class="language-plaintext highlighter-rouge">--interactive</code> flag.
The fact that they’re often done in the same workflow can add to the confusion.</p>

<h3 id="final-note">Final note</h3>

<p>Whether to merge or rebase is a question each organization needs to answer for themselves.
The debate over which approach to take can sometimes result in levels of vehemence only seen in arguments over tabs vs. spaces, and similar important questions.</p>

<p>In our organization, we take a compromised approach.
If a PR has one commit, then the answer is easy: rebase and avoid a merge commit.
The PR and comment are self-contained, so avoid the clutter.</p>

<p>If a PR has several significant commits, then just do a merge commit.
This preserves the branch structure.
The progression of work can be seen in isolation.
The merge commit can also contain the original branch name in the comment, which is lost after a merge when using git, due to the ephemeral nature of branches.
This can be a plus for some people.</p>

<p>Finally, if the PR has more than one commit, but most are tiny or inconsequential, then the history of those commits isn’t that important.
In this case, it makes sense to squash down to one commit on the branch and then rebase onto master.</p>

<p>This approach has worked well for us.
It combines the best of both philosophies—preserving history and branch structure when it’s important, and cleaning up when it’s not.</p>]]></content><author><name></name></author><summary type="html"><![CDATA[A feature or topic branch based workflow is an essential part of modern software development. A topic branch gives a developer an isolated place to do work. And once that work is done, it provides a basis for creating a pull request.]]></summary></entry><entry><title type="html">React and TypeScript</title><link href="/2015/11/react-typescript/" rel="alternate" type="text/html" title="React and TypeScript" /><published>2015-11-14T00:00:00-08:00</published><updated>2015-11-14T00:00:00-08:00</updated><id>/2015/11/react-typescript</id><content type="html" xml:base="/2015/11/react-typescript/"><![CDATA[<p><a href="http://www.typescriptlang.org/">TypeScript</a> was recently updated to version 1.6, which adds support for <a href="https://facebook.github.io/react/docs/jsx-in-depth.html">React JSX syntax</a>.
I’ve put together a <a href="https://github.com/toddlucas/react-tsx-starter">starter project</a> on Github that makes use of it.</p>

<p>Whenever I have an idea for a project that I’d like to prototype, it’s nice to be able to experiment with it quickly.
Getting Node.js set up with all the pieces that I like to play with can be a bit time consuming.
This starter takes that process down to a matter of minutes.</p>

<h2 id="isomorphism">Isomorphism</h2>

<p>One of the big reasons that many people use Node is that the same code can run on the server and in the browser.
React supports this so-called isomorphic modality well.
To achieve isomorphism, a few things are necessary.</p>

<p>First, rendering on the client and server must be supported.
React is primarily a client-side framework, but it supports rendering on the server with the <code class="language-plaintext highlighter-rouge">renderToString</code> method.</p>

<p>Next, the code must be packaged differently for the browser.
This starter project uses <a href="http://browserify.org/">Browserify</a>, which allows modules written for the Node environment to be converted for use in the browser.
It locates dependencies and packages up all relevant modules into a single JavaScript file, with proper module isolation.</p>

<p>Finally, the server must be able to render any page when the user refreshes, or navigates directly.
<a href="https://github.com/rackt/react-router">React Router</a> is the standard here.
It allows pseudo-navigation client side using the <a href="http://www.w3.org/TR/html5/browsers.html#history">HTML5 History API</a>, as specified in a routing table.
If a page is refreshed, the same routing table can be used on the server to render the equivalent page.</p>

<h2 id="tsx">TSX</h2>

<p>The new JSX support in TypeScript 1.6 is a big change.
When you combine TypeScript with React JSX syntax, you can use the new <code class="language-plaintext highlighter-rouge">.tsx</code> file extension.
Building React components using ES6 syntax with type checking is a major improvement.
Tools like <a href="https://code.visualstudio.com/">Visual Studio Code</a> can take advantage of the compiler and typing information to provide code completion.
This makes writing JavaScript a much more productive and positive experience for those of us who are used to this editing environment.
Here’s an example:</p>

<div class="language-jsx highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">import</span> <span class="o">*</span> <span class="nx">as</span> <span class="nx">React</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span>

<span class="k">export</span> <span class="k">default</span> <span class="kd">class</span> <span class="nc">AboutView</span> <span class="kd">extends</span> <span class="nc">React</span><span class="p">.</span><span class="nx">Component</span><span class="o">&lt;</span><span class="nx">any</span><span class="p">,</span> <span class="nx">any</span><span class="o">&gt;</span> <span class="p">{</span>
    <span class="nf">render</span><span class="p">()</span> <span class="p">{</span>
        <span class="k">return</span> <span class="p">&lt;</span><span class="nt">div</span><span class="p">&gt;</span>
            <span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>Example<span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
        <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;;</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre></div></div>

<p>If your component takes props or state, you can define an interface to represent them.
The two <code class="language-plaintext highlighter-rouge">any</code> template parameters provide defaults.</p>

<div class="language-jsx highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">import</span> <span class="o">*</span> <span class="nx">as</span> <span class="nx">React</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span>

<span class="k">export</span> <span class="kr">interface</span> <span class="nx">IAboutViewProps</span> <span class="p">{</span>
    <span class="nl">title</span><span class="p">:</span> <span class="nx">string</span><span class="p">;</span>
<span class="p">}</span>

<span class="k">export</span> <span class="k">default</span> <span class="kd">class</span> <span class="nc">AboutView</span> <span class="kd">extends</span> <span class="nc">React</span><span class="p">.</span><span class="nx">Component</span><span class="o">&lt;</span><span class="nx">IAboutViewProps</span><span class="p">,</span> <span class="nx">any</span><span class="o">&gt;</span> <span class="p">{</span>
    <span class="nf">render</span><span class="p">()</span> <span class="p">{</span>
        <span class="k">return</span> <span class="p">&lt;</span><span class="nt">div</span><span class="p">&gt;</span>
            <span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span><span class="si">{</span><span class="k">this</span><span class="p">.</span><span class="nx">props</span><span class="p">.</span><span class="nx">title</span><span class="si">}</span><span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
        <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;;</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre></div></div>

<p>If you don’t use the expected props, you’ll get a compile-time error.
Also, if a parent doesn’t specify a required prop, the compiler will tell you that it’s missing.
This can be very useful as a project grows or if components are refactored.</p>

<h2 id="building">Building</h2>

<p>The starter project uses <a href="http://gulpjs.com/">Gulp</a> as the build system.
It includes a gulpfile that has a number of nice features.
All the source is in one directory, <code class="language-plaintext highlighter-rouge">src</code>.
During the build, this is transformed into a parallel directory, called <code class="language-plaintext highlighter-rouge">www</code>.</p>

<p>Static files are copied, and JS and CSS files are minified.
If you use <a href="http://lesscss.org/">Less</a>, the <code class="language-plaintext highlighter-rouge">.less</code> files are compiled and minified.</p>

<h3 id="browserification">Browserification</h3>

<p>As mentioned earlier, the build process browserifies the client code.
Some libraries can be referenced externally, via globals.
jQuery is a good example, with its <code class="language-plaintext highlighter-rouge">$</code> object.
Other libraries are referenced through <code class="language-plaintext highlighter-rouge">requires</code> or <code class="language-plaintext highlighter-rouge">import</code> statements.
Any such reference will normally cause the entire library to be pulled in.
This can result a huge download and a slow build process.</p>

<p>This gulpfile breaks the build into three parts.
First, required 3rd party libraries are browserified into a separate <code class="language-plaintext highlighter-rouge">vendor.js</code> file.
Second, certain 3rd party libraries, such as jQuery, are excluded using <code class="language-plaintext highlighter-rouge">browserify-shim</code>.
These files are included via normal <code class="language-plaintext highlighter-rouge">script</code> elements.
Making a library external like this requires changes in three places:</p>

<ul>
  <li>An entry in extern_js in gulpfile.js</li>
  <li>An entry under browserify-shim in package.json</li>
  <li>A script reference</li>
</ul>

<p>Finally, the remainder of the client files are packaged into <code class="language-plaintext highlighter-rouge">app.js</code>.
This focus means that only one file needs to be rebuilt most of the time.</p>

<h3 id="watchers">Watchers</h3>

<p>Any time file is edited, a Gulp watcher will just build the piece that changed.
This can help minimize the edit/reload cycle time.
Keeping <code class="language-plaintext highlighter-rouge">app.js</code> small also helps.</p>

<h2 id="try-it-out">Try it out</h2>

<p>I hope that this will be useful to others who want to try TypeScript with React.
Let me know what you think.</p>]]></content><author><name></name></author><summary type="html"><![CDATA[TypeScript was recently updated to version 1.6, which adds support for React JSX syntax. I’ve put together a starter project on Github that makes use of it.]]></summary></entry><entry><title type="html">Client Side ModelState</title><link href="/2014/08/client-side-modelstate/" rel="alternate" type="text/html" title="Client Side ModelState" /><published>2014-08-09T00:00:00-07:00</published><updated>2014-08-09T00:00:00-07:00</updated><id>/2014/08/client-side-modelstate</id><content type="html" xml:base="/2014/08/client-side-modelstate/"><![CDATA[<p>ASP.NET MVC has good capabilities for handling model state validation.
Making use of model attributes, such as <code class="language-plaintext highlighter-rouge">Required</code>, it’s pretty easy to put together a validating form quickly.
MVC 3 introduced unobtrusive validation, which enables client-side validation prior to form post.
The HTML helpers render additional mark-up to transparently assist.</p>

<p>There has been a steady move over the past several years of applications relying more and more on JavaScript.
Whereas you would often hear terms like <em><a href="http://en.wikipedia.org/wiki/Progressive_enhancement">progressive enhancement</a></em> or <em>graceful degradation</em>, you’re now more likely to hear <em><a href="http://en.wikipedia.org/wiki/Single-page_application">single page app</a></em>.
Libraries like <a href="http://knockoutjs.com/">KnockoutJS</a> and <a href="https://angularjs.org/">AngularJS</a> are becoming very popular.</p>

<p>A middle-ground approach is to begin moving some server side flows to a more client-first approach.
A good candidate for this migration is the simple form.
Whereas a typical form might use <a href="http://en.wikipedia.org/wiki/Post/Redirect/Get">post/redirect/get</a>,
a client-first approach might use a dialog with the embedded form rendered with a partial.
If you attempt to move to a more client-first approach, you’ll quickly find that much of the validation capability built into MVC is lost.</p>

<h3 id="web-api-2">Web API 2</h3>

<p>Another great recent advancement in the ASP.NET stack is <a href="http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api">Web API 2</a>.
One of the interesting methods defined for the <code class="language-plaintext highlighter-rouge">ApiController</code> base class is <code class="language-plaintext highlighter-rouge">BadRequest</code>.</p>

<div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">[</span><span class="n">HttpPost</span><span class="p">]</span>
<span class="k">public</span> <span class="n">IHttpActionResult</span> <span class="nf">Post</span><span class="p">(</span><span class="n">CheckDatesModel</span> <span class="n">model</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">if</span> <span class="p">(!</span><span class="n">ModelState</span><span class="p">.</span><span class="n">IsValid</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="nf">BadRequest</span><span class="p">(</span><span class="n">ModelState</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="p">...</span>
<span class="p">}</span>
</code></pre></div></div>

<p>This method will return any <code class="language-plaintext highlighter-rouge">ModelState</code> errors to the client as JSON:</p>

<div class="language-json highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">{</span><span class="w">
    </span><span class="nl">"Message"</span><span class="p">:</span><span class="w"> </span><span class="s2">"The request is invalid."</span><span class="p">,</span><span class="w">
    </span><span class="nl">"ModelState"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
        </span><span class="nl">"model.StartDate"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="s2">"The Start Date field is required."</span><span class="w"> </span><span class="p">],</span><span class="w">
        </span><span class="nl">"model.EndDate"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="s2">"The End Date field is required."</span><span class="w"> </span><span class="p">]</span><span class="w">
    </span><span class="p">}</span><span class="w">
</span><span class="p">}</span><span class="w">
</span></code></pre></div></div>

<p>Unfortunately, there doesn’t appear to be any built-in mechanism to support this data on the client.
Regardless, we can make use of this data to do our own client side validation using existing structures.
A great example of this is written about in <a href="http://brettedotnet.wordpress.com/2013/05/01/asp-net-web-api-validation-a-one-more-better-approach/">ASP.NET Web API Validation</a>.
More on that later.</p>

<h2 id="server-side">Server side</h2>

<p>It would be great if we had a comprehensive approach that would allow us to use the Web API mechanism, or to use a similar mechanism on the MVC side.
All we really need are two pieces.
The client side piece, for which <a href="http://brettedotnet.wordpress.com/2013/05/01/asp-net-web-api-validation-a-one-more-better-approach/">brette</a> has shown us the way, and a server-side complement to the Web API mechanism for use with MVC.
The latter can be accomplished pretty easily using an extension method on the ModelStateDictionary class.
Here’s an example, written in <em>one line</em> of code :)</p>

<div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">public</span> <span class="k">static</span> <span class="k">class</span> <span class="nc">ModelStateDictionaryExtensions</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="k">static</span> <span class="n">Dictionary</span><span class="p">&lt;</span><span class="kt">string</span><span class="p">,</span> <span class="kt">string</span><span class="p">[</span><span class="k">]&gt;</span> <span class="nf">GetErrors</span><span class="p">(</span>
        <span class="k">this</span> <span class="n">ModelStateDictionary</span> <span class="n">modelState</span><span class="p">,</span> 
        <span class="kt">string</span> <span class="n">prefix</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="n">modelState</span>
            <span class="p">.</span><span class="nf">Where</span><span class="p">(</span><span class="n">kvp</span> <span class="p">=&gt;</span> <span class="n">kvp</span><span class="p">.</span><span class="n">Value</span><span class="p">.</span><span class="n">Errors</span><span class="p">.</span><span class="n">Count</span> <span class="p">&gt;</span> <span class="m">0</span><span class="p">)</span>
            <span class="p">.</span><span class="nf">ToDictionary</span><span class="p">(</span><span class="n">kvp</span> <span class="p">=&gt;</span> <span class="n">String</span><span class="p">.</span><span class="nf">IsNullOrWhiteSpace</span><span class="p">(</span><span class="n">kvp</span><span class="p">.</span><span class="n">Key</span><span class="p">)</span>
                                <span class="p">?</span> <span class="n">String</span><span class="p">.</span><span class="n">Empty</span> 
                                <span class="p">:</span> <span class="n">prefix</span> <span class="p">==</span> <span class="k">null</span> 
                                    <span class="p">?</span> <span class="n">kvp</span><span class="p">.</span><span class="n">Key</span>
                                    <span class="p">:</span> <span class="n">prefix</span><span class="p">.</span><span class="nf">TrimEnd</span><span class="p">(</span><span class="sc">'.'</span><span class="p">)</span> <span class="p">+</span> <span class="s">"."</span> <span class="p">+</span> <span class="n">kvp</span><span class="p">.</span><span class="n">Key</span><span class="p">,</span>
                            <span class="n">kvp</span> <span class="p">=&gt;</span> <span class="n">kvp</span><span class="p">.</span><span class="n">Value</span><span class="p">.</span><span class="n">Errors</span>
                                    <span class="p">.</span><span class="nf">Select</span><span class="p">(</span><span class="n">e</span> <span class="p">=&gt;</span> <span class="n">e</span><span class="p">.</span><span class="n">ErrorMessage</span><span class="p">)</span>
                                    <span class="p">.</span><span class="nf">ToArray</span><span class="p">());</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre></div></div>

<p>To use the extension method from MVC, we would do something like this:</p>

<div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">[</span><span class="n">HttpPost</span><span class="p">,</span> <span class="n">ValidateAntiForgeryToken</span><span class="p">]</span>
<span class="k">public</span> <span class="n">ActionResult</span> <span class="nf">CheckDatesJson</span><span class="p">(</span><span class="n">CheckDatesModel</span> <span class="n">model</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">if</span> <span class="p">(!</span><span class="n">ModelState</span><span class="p">.</span><span class="n">IsValid</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">Response</span><span class="p">.</span><span class="n">StatusCode</span> <span class="p">=</span> <span class="m">400</span><span class="p">;</span>
        <span class="k">return</span> <span class="nf">Json</span><span class="p">(</span><span class="k">new</span> <span class="p">{</span> 
            <span class="n">Message</span> <span class="p">=</span> <span class="s">"The request is invalid."</span><span class="p">,</span>
            <span class="n">ModelState</span> <span class="p">=</span> <span class="n">ModelState</span><span class="p">.</span><span class="nf">GetErrors</span><span class="p">(</span><span class="s">"model"</span><span class="p">)</span> 
        <span class="p">});</span>
    <span class="p">}</span>
    <span class="p">...</span>
<span class="p">}</span>
</code></pre></div></div>

<p>This produces the identical format as the equivalent Web API method:</p>

<div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">[</span><span class="n">HttpPost</span><span class="p">]</span>
<span class="k">public</span> <span class="n">IHttpActionResult</span> <span class="nf">Post</span><span class="p">(</span><span class="n">CheckDatesModel</span> <span class="n">model</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">if</span> <span class="p">(!</span><span class="n">ModelState</span><span class="p">.</span><span class="n">IsValid</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="nf">BadRequest</span><span class="p">(</span><span class="n">ModelState</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="p">...</span>
<span class="p">}</span>
</code></pre></div></div>

<p>It would be pretty easy to wrap the Json and GetError calls into a method on a base class derived from Controller.
One might even call it BadRequest.
Although, hopefully, this new version would have a way to override the default Message.</p>

<h2 id="client-side">Client side</h2>

<p>The client side is a bit more complicated. 
It involves taking the JSON result and applying it to the existing validation mark-up.</p>

<h3 id="validation-summary">Validation Summary</h3>

<p>One of the first pieces to address is the <code class="language-plaintext highlighter-rouge">Html.ValidationSummary()</code> method.
This method emits different markup depending on how it’s called.
The default <code class="language-plaintext highlighter-rouge">Html.ValidateSummary()</code> will render all errors, both model-level and errors that are not associated with a model.
Other versions allow for the suppression of model-level errors by passing <code class="language-plaintext highlighter-rouge">true</code> for the <code class="language-plaintext highlighter-rouge">excludePropertyErrors</code> argument.
These two versions render different HTML.
However, this can be accommodated-for on the client.</p>

<p>In the default case, <code class="language-plaintext highlighter-rouge">Html.ValidationSummary()</code> renders this on initial form render, with the <code class="language-plaintext highlighter-rouge">data-valmsg-summary="true"</code> attribute.</p>

<div class="language-html highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">"validation-summary-valid"</span> <span class="na">data-valmsg-summary=</span><span class="s">"true"</span><span class="nt">&gt;</span>
    <span class="nt">&lt;ul&gt;</span>
        <span class="nt">&lt;li</span> <span class="na">style=</span><span class="s">"display:none"</span><span class="nt">&gt;&lt;/li&gt;</span>
    <span class="nt">&lt;/ul&gt;</span>
<span class="nt">&lt;/div&gt;</span>
</code></pre></div></div>

<p>After the form post, if there are errors, they will be listed.
In addition, the class will change from <code class="language-plaintext highlighter-rouge">validation-summary-valid</code> to <code class="language-plaintext highlighter-rouge">validation-summary-errors</code>.</p>

<p>One case that is problematic is when <code class="language-plaintext highlighter-rouge">true</code> is passed for <code class="language-plaintext highlighter-rouge">excludePropertyErrors</code>.
When <code class="language-plaintext highlighter-rouge">Html.ValidateSummary(true)</code> is specified, <em>no HTML is emitted</em> on the initial form render. 
After a form post with errors, the result is the same as the default case, but without the <code class="language-plaintext highlighter-rouge">data-valmsg-summary="true"</code> attribute.</p>

<p>One solution would be to add a wrapper element which will always be present.
This is the approach taken here, although it’s optional.</p>

<div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nf">@using</span> <span class="p">(</span><span class="n">Html</span><span class="p">.</span><span class="nf">BeginForm</span><span class="p">())</span> 
<span class="p">{</span>
    <span class="n">@Html</span><span class="p">.</span><span class="nf">AntiForgeryToken</span><span class="p">()</span>
    <span class="p">&lt;</span><span class="n">div</span> <span class="k">class</span><span class="err">="</span><span class="nc">validation</span><span class="p">-</span><span class="n">summary</span><span class="s">"&gt;
</span>        <span class="n">@Html</span><span class="p">.</span><span class="nf">ValidationSummary</span><span class="p">(</span><span class="k">true</span><span class="p">)</span>
    <span class="p">&lt;/</span><span class="n">div</span><span class="p">&gt;</span>
    <span class="p">...</span>
<span class="p">}</span>
</code></pre></div></div>

<p>This wrapper element allows us to construct validation error mark-up regardless of whether it was emitted by <code class="language-plaintext highlighter-rouge">Html.ValidationSummary()</code> or not.</p>

<h3 id="ajax-and-render">Ajax and Render</h3>

<p>The last step is to make the Ajax call.</p>

<div class="language-js highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">&lt;</span><span class="nx">script</span> <span class="nx">src</span><span class="o">=</span><span class="dl">"</span><span class="s2">~/scripts/app/modelstate.js</span><span class="dl">"</span><span class="o">&gt;&lt;</span><span class="sr">/script</span><span class="err">&gt;
</span><span class="o">&lt;</span><span class="nx">script</span><span class="o">&gt;</span>
<span class="nf">$</span><span class="p">(</span><span class="nf">function </span><span class="p">()</span> <span class="p">{</span>
    <span class="kd">var</span> <span class="nx">jForm</span> <span class="o">=</span> <span class="nf">$</span><span class="p">(</span><span class="dl">"</span><span class="s2">form</span><span class="dl">"</span><span class="p">);</span>
    
    <span class="nx">jForm</span><span class="p">.</span><span class="nf">submit</span><span class="p">(</span><span class="nf">function </span><span class="p">(</span><span class="nx">event</span><span class="p">)</span> <span class="p">{</span>
        <span class="nx">event</span><span class="p">.</span><span class="nf">preventDefault</span><span class="p">();</span>
        
        <span class="c1">// Clear any previous errors.</span>
        <span class="nx">App</span><span class="p">.</span><span class="nx">ModelState</span><span class="p">.</span><span class="nf">clearErrors</span><span class="p">(</span><span class="nx">jForm</span><span class="p">);</span>

        <span class="nx">$</span><span class="p">.</span><span class="nf">ajax</span><span class="p">({</span>
            <span class="na">url</span><span class="p">:</span> <span class="dl">'</span><span class="s1">/mycontroller/checkdatesjson</span><span class="dl">'</span><span class="p">,</span>
            <span class="na">data</span><span class="p">:</span> <span class="nf">$</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nf">serializeArray</span><span class="p">(),</span>
            <span class="na">type</span><span class="p">:</span> <span class="dl">'</span><span class="s1">POST</span><span class="dl">'</span><span class="p">,</span>
            <span class="na">success</span><span class="p">:</span> <span class="nf">function </span><span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">{</span>
                <span class="c1">// Do something</span>
            <span class="p">},</span>
            <span class="na">statusCode</span><span class="p">:</span> <span class="p">{</span>
                <span class="mi">400</span><span class="p">:</span> <span class="nf">function </span><span class="p">(</span><span class="nx">jqXHR</span><span class="p">)</span> <span class="p">{</span>
                    <span class="c1">// Deserialize and render the ModelState.</span>
                    <span class="nx">App</span><span class="p">.</span><span class="nx">ModelState</span><span class="p">.</span><span class="nf">showResponseErrors</span><span class="p">(</span><span class="nx">jForm</span><span class="p">,</span> <span class="nx">jqXHR</span><span class="p">);</span>
                <span class="p">}</span>
            <span class="p">}</span>
        <span class="p">});</span>
    <span class="p">});</span>
<span class="p">});</span>
<span class="o">&lt;</span><span class="sr">/script</span><span class="err">&gt;
</span></code></pre></div></div>

<p>The implementation, based on <a href="http://brettedotnet.wordpress.com/2013/05/01/asp-net-web-api-validation-a-one-more-better-approach/">brette’s</a>, is written in TypeScript.
It can be found on GitHub <a href="https://github.com/ptoinc/client-modelstate">here</a>.</p>

<h2 id="applicability">Applicability</h2>

<p>This method of moving halfway to client-side processing is best used in places where you still want to use the great features of MVC, but want more client-side interactivity.
By using this component, any new client-side form handling can have the same presentation as your existing server-side form handling, allowing for a consistent user experience.</p>]]></content><author><name></name></author><summary type="html"><![CDATA[ASP.NET MVC has good capabilities for handling model state validation. Making use of model attributes, such as Required, it’s pretty easy to put together a validating form quickly. MVC 3 introduced unobtrusive validation, which enables client-side validation prior to form post. The HTML helpers render additional mark-up to transparently assist.]]></summary></entry><entry><title type="html">Starting Ocam</title><link href="/2012/02/starting-ocam/" rel="alternate" type="text/html" title="Starting Ocam" /><published>2012-02-25T00:00:00-08:00</published><updated>2012-02-25T00:00:00-08:00</updated><id>/2012/02/starting-ocam</id><content type="html" xml:base="/2012/02/starting-ocam/"><![CDATA[<p>I started working on Ocam almost two weeks ago.
I’ve had a need recently for a way of writing documentation for a couple of projects I’ve been working on. 
I didn’t want to have to write a bunch of server code or to have to install a database to back a blog engine.
I wanted something simple and elegant.</p>

<p>I recently tried out <a href="http://jashkenas.github.com/docco/">Docco</a> because I had seen it used on a few JavaScript projects and the literate programming attributes had a certain appeal. 
I used it on my own project and liked the result, so I was looking for something similar.</p>

<p>In evaluating different documentation systems I like, I’ve come across several nice examples, including
<a href="http://documentcloud.github.com/backbone/">Backbone</a> and 
<a href="http://docs.orchardproject.net/">Orchard</a>. 
I was curious about how they approached their documentation.
I little research lead me to the <a href="https://github.com/OrchardCMS/OrchardDoc">OrchardDoc</a> project on GitHub.</p>

<p>The OrchardDoc project is mainly documentation content, as you might expect.
I didn’t find much code there.
There were a couple of things worth noting, though. 
The first is that Markdown is the primary means of authoring content.
The second is that it requires ASP.NET to run.
I liked the Markdown approach, but I was looking for something that didn’t have server side dependencies.</p>

<p>Based on one of the assemblies in the Bin directory, it appeared to use <a href="https://github.com/NuGet/NuGetDocs">NuGetDocs</a>. 
A little searching lead to 
<a href="http://weblogs.asp.net/bleroy/archive/2011/10/25/from-screwturn-wiki-to-markdown.aspx">this post</a>
which confirmed it.
The NuGetDocs project makes interesting use of the <a href="http://code.google.com/p/markdownsharp/">MarkdownSharp</a> component to allow Markdown to be used in an ASP.NET environment.</p>

<p>So here were two two impressive .NET projects and they were both using a Markdown based documentation system. 
I started to look for alternatives that didn’t have the server side dependency.</p>

<p><a href="https://github.com/mojombo/jekyll/wiki">Jekyll</a> is probably the best known Markdown site generator. 
It’s used by GitHub and was written by the Cofounder and CTO of GitHub, <a href="http://tom.preston-werner.com/">Tom Preston-Werner</a>.
I installed it and played with it a bit. 
Although it’s written in Ruby, it uses Pygments, which is written in Python, for syntax highlighting.</p>

<p>After using it for a little while, I started wanting to make modifications. 
Then I got to thinking about how great it would be to have a system like Jekyll that used the idioms that I was familiar with from ASP.NET MVC.
In particular, I really like the Razor view engine and their approach to code and markup.
I had known about the <a href="http://razorengine.codeplex.com/">RazorEngine</a> 
project, so I started looking into the possibility of writing a stand-alone 
console app that would have all the features I wanted in a simple package.</p>

<p>I had been thinking of writing a content platform for some time,
but I always abandoned the idea because there isn’t a great syntax 
highlighting package for the .NET platform.
The fact that Jekyll called out to Pygments, which is written in a different language, 
was a kind of permission for me to go forward.</p>

<p>I uploaded <a href="http://toddlucas.github.com/ocam/">the result</a> about two weeks ago.
It’s still a work in progress.
Please feel free to contact me with questions or comments.</p>]]></content><author><name></name></author><category term="Programming" /><category term="C#" /><category term=".NET" /><summary type="html"><![CDATA[I started working on Ocam almost two weeks ago. I’ve had a need recently for a way of writing documentation for a couple of projects I’ve been working on. I didn’t want to have to write a bunch of server code or to have to install a database to back a blog engine. I wanted something simple and elegant.]]></summary></entry></feed>