scratch/output/Scratch/en/blog/2010-03-23-Encapsulate-git/index.html

358 lines
14 KiB
HTML
Raw Normal View History

2011-04-20 12:29:01 +00:00
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="fr" xml:lang="fr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="keywords" content="git, protection, branches, diverged">
<link rel="shortcut icon" type="image/x-icon" href="/Scratch/img/favicon.ico" />
<link rel="stylesheet" type="text/css" href="/Scratch/assets/css/main.css" />
2012-04-02 21:43:39 +00:00
<link rel="stylesheet" type="text/css" href="/Scratch/css/solarized.css" />
<link rel="stylesheet" type="text/css" href="/Scratch/css/idc.css" />
2012-05-02 15:43:56 +00:00
<link href='http://fonts.googleapis.com/css?family=Inconsolata' rel='stylesheet' type='text/css'>
2011-04-20 12:29:01 +00:00
<link rel="alternate" type="application/rss+xml" title="RSS" href="http://feeds.feedburner.com/yannespositocomen"/>
<link rel="alternate" lang="fr" xml:lang="fr" title="Encapsuler git" type="text/html" hreflang="fr" href="/Scratch/fr/blog/2010-03-23-Encapsulate-git/" />
<link rel="alternate" lang="en" xml:lang="en" title="Encapsulate git" type="text/html" hreflang="en" href="/Scratch/en/blog/2010-03-23-Encapsulate-git/" />
<script type="text/javascript" src="/Scratch/js/jquery-1.3.1.min.js"></script>
<script type="text/javascript" src="/Scratch/js/jquery.cookie.js"></script>
<script type="text/javascript" src="/Scratch/js/index.js"></script>
2012-05-02 15:43:56 +00:00
<script type="text/javascript" src="/Scratch/js/highlight/highlight.pack.js"></script>
<script type="text/javascript" src="/Scratch/js/article.js"></script>
2011-04-20 12:29:01 +00:00
<!--[if lt IE 9]>
<script src="http://ie7-js.googlecode.com/svn/version/2.1(beta4)/IE9.js"></script>
<![endif]-->
<title>Encapsulate git</title>
</head>
2011-10-18 22:30:00 +00:00
<body lang="en" class="article">
2011-04-20 12:29:01 +00:00
<script type="text/javascript">// <![CDATA[
document.write('<div id="blackpage"><img src="/Scratch/img/loading.gif" alt="loading..."/></div>');
2011-04-20 12:29:01 +00:00
// ]]>
</script>
<div id="content">
<div id="choix">
<div class="return"><a href="#entete">&darr; Menu &darr;</a></div>
<div id="choixlang">
<a href="/Scratch/fr/blog/2010-03-23-Encapsulate-git/" onclick="setLanguage('fr')">en Français</a>
2011-04-20 12:29:01 +00:00
</div>
2011-09-28 16:05:55 +00:00
<div class="flush"></div>
2011-04-20 12:29:01 +00:00
</div>
<div id="titre">
<h1>
Encapsulate git
</h1>
</div>
<div class="flush"></div>
<div class="flush"></div>
<div id="afterheader">
<div class="corps">
<div class="intro">
Here is a solution to maintain divergent branches in git. Because it is easy to merge by mistake. I give a script that encapsulate git in order to forbid some merge and warn you some merge should be dangerous.
</div>
<h2 id="how-to-protect-against-your-own-dumb">how to protect against your own dumb</h2>
<p>I work on a project in which some of my git branches should remain divergent. And divergences should grow.</p>
<p>I also use some branch to contain what is common between projects.</p>
<p>Say I have some branches:</p>
<p>master: common to all branches
dev: branch devoted to unstable development
client: branch with features for all client but not general enough for master
clientA: project adapted for client A
clientB: project adapted for client B</p>
<p>Here how I want to work: </p>
<p><img alt="Dynamic branching" src="/Scratch/img/blog/2010-03-23-Encapsulate-git/dynamic_branching.png" /></p>
2011-04-20 12:29:01 +00:00
<p>And more precisely the branch hierarchy: </p>
<p><img alt="Branch hierarchy" src="/Scratch/img/blog/2010-03-23-Encapsulate-git/branch_hierarchy.png" /></p>
2011-04-20 12:29:01 +00:00
<p>An arrow from A to B means, you can merge A in B. If there is no arrow from A to B that means it is <em>forbidden</em> to merge A in B. Here is the corresponding rubycode:</p>
2012-05-02 15:43:56 +00:00
<div>
<pre><code class="ruby">$architecture={
:master =&gt; [ :dev, :client ],
:dev =&gt; [ :master ],
:client =&gt; [ :clientA, :clientB ] }
</code></pre>
</div>
2011-04-20 12:29:01 +00:00
<p>Having a <code>:master =&gt; [ :dev, :client ]</code> means you can merge <code>master</code> branch into <code>dev</code> and <code>client</code>.</p>
<p>If by mistake I make a <code>git checkout master &amp;&amp; git merge clientA</code>, I made a mistake. This is why I made a script which encapsulate the git behaviour to dodge this kind of mistake.</p>
<p>But this script do far more than that. It also merge from top to down. The action <code>allmerges</code> will do:</p>
2012-05-02 15:43:56 +00:00
<div>
<pre><code class="zsh">git co dev &amp;&amp; git merge master
git co client &amp;&amp; git merge master
git co clientA &amp;&amp; git merge client
git co clientB &amp;&amp; git merge client
</code></pre>
</div>
2011-04-20 12:29:01 +00:00
<p>That means, I can update all branches. The algorithm will not make loop even if there is a cycle in the branch hierarchy.</p>
<p>Here it is:</p>
2012-05-02 15:43:56 +00:00
<div class="small"><div class="codefile"><a href="/Scratch/en/blog/2010-03-23-Encapsulate-git/code/eng">&#x27A5; eng</a></div>
<pre><code class="ruby">#!/usr/bin/env ruby
# encoding: utf-8
# architecture
#
# master &lt;-&gt; dev
# master -&gt; client
# clien -&gt; clientA | clientB
#
# merge using two of these branches should be
# restricted to these rules
# merge to one of these branch and an unknown one should
# raise a warning, and may the option to add this new branch
# to the hierarchy
$architecture={
:master =&gt; [ :dev, :client ],
:dev =&gt; [ :master ],
:client =&gt; [ :clientA, :clientB ] }
def get_current_branch()
(`git branch --no-color | awk '$1 == "*" {print $2}'`).chop.intern
end
if ARGV.length == 0
puts %{usage: $0:t [git_command or local_command]
local commands:
allmerges: merge from top to down}
exit 0
end
require 'set'
$known_branches=Set.new
$architecture.each do |k,v|
$known_branches.add(k)
v.each { |b| $known_branches.add(b) }
end
def rec_merge(branch)
if $architecture[branch].nil?
return
end
$architecture[branch].each do |b|
if $flag.has_key?(b.to_s + branch.to_s)
next
end
flagname=branch.to_s + b.to_s
if $flag.has_key?(flagname)
next
end
if system %{eng checkout #{b}}
if get_current_branch != b
puts "Can't checkout to #{b}"
exit 2
end
if system %{eng merge #{branch}}
$flag[flagname]=true
rec_merge(b)
else
exit 1
end
else
exit 1
end
end
end
def do_all_merges
puts 'Will merge from father to sons'
current_branch=get_current_branch
$flag={}
rec_merge(:master)
system %{git co #{current_branch}}
end
def do_merge
current_branch=get_current_branch
src_branch=ARGV[1].intern
puts %{do_merge: #{src_branch} =&gt; #{current_branch}}
if $known_branches.include?(current_branch)
if $known_branches.include?(src_branch)
if $architecture.has_key?(src_branch) and
$architecture[src_branch].include?(current_branch)
system %{git merge #{src_branch}}
else
puts %{Forbidden merge: #{src_branch} =&gt; #{current_branch}}
end
else
puts %{Warning! #{src_branch} not mentionned in rb configuration}
sleep 2
system %{git merge #{src_branch}}
puts %{Warning! #{src_branch} not mentionned in rb configuration}
end
end
end
case ARGV[0]
when 'allmerges' then do_all_merges
when 'merge' then do_merge
else system %{git #{ARGV.join(' ')}}
end
</code></pre>
</div>
2011-04-20 12:29:01 +00:00
<p>All you need to do to make it work is simply to copy eng in a directory contained in your PATH.</p>
<p>Of course try to use as few as possible <code>cherry-pick</code> and <code>rebase</code>. This script was intended to work with workflow using <code>pull</code> and <code>merge</code>.</p>
</div>
2012-04-10 13:56:34 +00:00
<div id="social">
<div class="left"> <a href="https://twitter.com/share" class="twitter-share-button" data-via="yogsototh">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
</div>
<div class="left"> <div class="g-plusone" data-size="medium" data-annotation="inline" data-width="106"></div>
<script type="text/javascript">
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>
</div>
<div class="flush"></div>
</div>
2011-04-20 12:29:01 +00:00
<div id="choixrss">
<a id="rss" href="http://feeds.feedburner.com/yannespositocomen">
Subscribe
</a>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('#comment').hide();
$('#clickcomment').click(showComments);
});
function showComments() {
$('#comment').show();
$('#clickcomment').fadeOut();
}
2012-04-10 13:56:34 +00:00
document.write('<div id="clickcomment">Comments &amp; Share</div>');
2011-04-20 12:29:01 +00:00
</script>
<div class="flush"></div>
2012-04-10 13:56:34 +00:00
2011-04-20 12:29:01 +00:00
<div class="corps" id="comment">
<h2 class="first">comments</h2>
<noscript>
You must enable javascript to comment.
</noscript>
<script type="text/javascript">
var idcomments_acct = 'a307f0044511ff1b5cfca573fc0a52e7';
var idcomments_post_id = '/Scratch/en/blog/2010-03-23-Encapsulate-git/';
var idcomments_post_url = 'http://yannesposito.com/Scratch/en/blog/2010-03-23-Encapsulate-git/';
2011-04-20 12:29:01 +00:00
</script>
<span id="IDCommentsPostTitle" style="display:none"></span>
<script type='text/javascript' src='/Scratch/js/genericCommentWrapperV2.js'></script>
2011-04-20 12:29:01 +00:00
</div>
<div id="entete" class="corps_spaced">
<div id="liens">
<ul><li><a href="/Scratch/en/">Home</a></li>
<li><a href="/Scratch/en/blog/">Blog</a></li>
<li><a href="/Scratch/en/softwares/">Softwares</a></li>
<li><a href="/Scratch/en/about/">About</a></li></ul>
2011-04-20 12:29:01 +00:00
</div>
<div class="flush"></div>
<hr/>
<div id="next_before_articles">
<div id="previous_articles">
previous entries
<div class="previous_article">
<a href="/Scratch/en/blog/2010-03-22-Git-Tips/"><span class="nicer">«</span>&nbsp;Git Tips</a>
2011-04-20 12:29:01 +00:00
</div>
<div class="previous_article">
<a href="/Scratch/en/blog/2010-02-23-When-regexp-is-not-the-best-solution/"><span class="nicer">«</span>&nbsp;When regexp is not the best solution</a>
2011-04-20 12:29:01 +00:00
</div>
<div class="previous_article">
<a href="/Scratch/en/blog/2010-02-18-split-a-file-by-keyword/"><span class="nicer">«</span>&nbsp;split a file by keyword</a>
2011-04-20 12:29:01 +00:00
</div>
</div>
<div id="next_articles">
next entries
<div class="next_article">
<a href="/Scratch/en/blog/2010-05-17-at-least-this-blog-revive/">I live again!&nbsp;<span class="nicer">»</span></a>
2011-04-20 12:29:01 +00:00
</div>
<div class="next_article">
<a href="/Scratch/en/blog/2010-05-19-How-to-cut-HTML-and-repair-it/">How to repair a cutted XML?&nbsp;<span class="nicer">»</span></a>
2011-04-20 12:29:01 +00:00
</div>
<div class="next_article">
<a href="/Scratch/en/blog/2010-05-24-Trees--Pragmatism-and-Formalism/">Trees; Pragmatism and Formalism&nbsp;<span class="nicer">»</span></a>
2011-04-20 12:29:01 +00:00
</div>
</div>
<div class="flush"></div>
</div>
</div>
<div id="bottom">
2012-04-02 21:43:39 +00:00
<div>
2012-04-10 13:56:34 +00:00
<a href="https://twitter.com/yogsototh">Follow @yogsototh</a>
2012-04-02 21:43:39 +00:00
</div>
2011-04-20 12:29:01 +00:00
<div>
<a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">Copyright ©, Yann Esposito</a>
</div>
<div id="lastmod">
Created: 03/23/2010
2012-05-02 15:43:56 +00:00
Modified: 04/26/2012
2011-04-20 12:29:01 +00:00
</div>
<div>
Entirely done with
<a href="http://www.vim.org">Vim</a>
and
<a href="http://nanoc.stoneship.org">nanoc</a>
</div>
</div>
<div class="clear"></div>
</div>
</body>
</html>