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" >
2011-04-20 13:56:52 +00:00
< 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" / >
2011-04-20 13:56:52 +00:00
< 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/yannespositocomfr" / >
2011-04-20 13:56:52 +00:00
< 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 > Encapsuler git< / title >
< / head >
2011-10-18 22:30:00 +00:00
< body lang = "fr" class = "article" >
2011-04-20 12:29:01 +00:00
< script type = "text/javascript" > / / < ! [ C D A T A [
2011-04-20 13:56:52 +00:00
document.write('< div id = "blackpage" > < img src = "/Scratch/img/loading.gif" alt = "Chargement en cours..." / > < / div > ');
2011-04-20 12:29:01 +00:00
// ]]>
< / script >
< div id = "content" >
< div id = "choix" >
< div class = "return" > < a href = "#entete" > ↓ Menu ↓ < / a > < / div >
< div id = "choixlang" >
2011-04-20 13:56:52 +00:00
< a href = "/Scratch/en/blog/2010-03-23-Encapsulate-git/" onclick = "setLanguage('en')" > in English< / 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 >
Encapsuler git
< / h1 >
< / div >
< div class = "flush" > < / div >
< div class = "flush" > < / div >
< div id = "afterheader" >
< div class = "corps" >
< p > < span class = "intro" >
Voici une solution pour conserver des branches divergentes avec < code > git< / code > .
2012-05-03 09:21:34 +00:00
Parce qu’ il est facile de < em > merger< / em > par erreur, je propose un script qui encapsule le comportement de < code > git< / code > pour interdire certains < em > merges< / em > dangereux. Mais qui permet aussi de faire des merges en cascades de la racines vers les autres branches.
2011-04-20 12:29:01 +00:00
< / span > < / p >
< h2 id = "se-prmunir-contre-les-erreurs" > Se prémunir contre les erreurs< / h2 >
2012-05-03 09:21:34 +00:00
< p > Je travaille sur un projet dans lequel certaines de mes branches < code > git< / code > doivent rester divergentes. Et les divergences devraient aller en s’ accentuant.< / p >
2011-04-20 12:29:01 +00:00
2012-05-03 09:21:34 +00:00
< p > J’ utilise aussi certaines branches qui contiennent la partie commune de ces projets.< / p >
2011-04-20 12:29:01 +00:00
2012-05-03 09:21:34 +00:00
< p > Disons que j’ ai les branches :< / p >
2011-04-20 12:29:01 +00:00
2011-05-30 12:12:24 +00:00
< ul >
< li > master: commune à toutes les branches< / li >
< li > dev: branche instable pour le développement< / li >
< li > client: Branche commune à plusieurs clients< / li >
< li > clientA: le projet spécialisé pour le client A< / li >
< li > clientB: le projet spécialisé pour le client B< / li >
< / ul >
2011-04-20 12:29:01 +00:00
< p > Voilà comment je souhaiterai que ça fonctionne< / p >
2011-04-20 13:56:52 +00:00
< 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 > Et plus précisément la hiérarchie des branches :< / p >
2011-04-20 13:56:52 +00:00
< 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
2012-05-03 09:21:34 +00:00
< p > Une flèche de A vers B signifie que l’ on peut merger A dans B. S’ il n’ y a pas de flèche de A vers B cela signifie qu’ il est < em > interdit< / em > de merger A dans B. Voici le code ruby correspondant :< / p >
2011-04-20 12:29:01 +00:00
2012-05-02 15:43:56 +00:00
< div >
2011-04-20 12:29:01 +00:00
2012-05-02 15:43:56 +00:00
< pre > < code class = "ruby" > $architecture={
:master => [ :dev, :client ],
:dev => [ :master ],
:client => [ :clientA, :clientB ] }
< / code > < / pre >
< / div >
2012-05-03 09:21:34 +00:00
< p > < code > :master => [ :dev, :client ]< / code > signifie que l’ on peut merger la branche < code > master< / code > dans la branche < code > dev< / code > et la branche < code > client< / code > .< / p >
2011-04-20 12:29:01 +00:00
< p > Je fait une erreur si je tape < code > git checkout master & & git merge clientA< / code > .
2012-05-03 09:21:34 +00:00
C’ est pour éviter ça que j’ ai fait un script pour encapsuler le comportement de < code > git< / code > .< / p >
2012-05-02 15:43:56 +00:00
2012-05-03 09:21:34 +00:00
< p > Mais ce script fait bien plus que ça. Il fait des merges en cascade de la racine vers les feuilles avec l’ acion < code > allmerges< / code > .< / p >
2011-04-20 12:29:01 +00:00
2012-05-02 15:43:56 +00:00
< div >
2011-04-20 12:29:01 +00:00
2012-05-02 15:43:56 +00:00
< pre > < code class = "zsh" > git co dev & & git merge master
git co client & & git merge master
git co clientA & & git merge client
git co clientB & & git merge client
< / code > < / pre >
2011-04-20 12:29:01 +00:00
2012-05-02 15:43:56 +00:00
< / div >
2012-05-03 09:21:34 +00:00
< p > Je peux ainsi mettre à jour toutes les branches. L’ algorithme ne boucle pas même s’ il y a des cycles dans la structure des branches.< br / >
2011-04-20 12:29:01 +00:00
Le voici :< / p >
2012-05-02 15:43:56 +00:00
< div class = "small" > < div class = "codefile" > < a href = "/Scratch/fr/blog/2010-03-23-Encapsulate-git/code/eng" > ➥ eng< / a > < / div >
< pre > < code class = "ruby" > #!/usr/bin/env ruby
# encoding: utf-8
# architecture
#
# master < -> dev
# master -> client
# clien -> 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 => [ :dev, :client ],
:dev => [ :master ],
:client => [ :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} => #{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} => #{current_branch}}
end
else
puts %{Warning! #{src_branch} not mentionned in rb configuration}
sleep 2
f 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 > Pour que ça fonctionne il vous suffit de copier < code > eng< / code > dans un répertoire présent dans votre < code > PATH< / code > .< / p >
2012-05-03 09:21:34 +00:00
< p > Bien entendu, il faut essayer de faire aussi peu que possible des < code > cherry-pick< / code > et des < code > rebase< / code > . Ce script a pour but de s’ intégrer avec les workflow basé sur les < code > pull< / code > et < code > merge< / code > .< / p >
2011-04-20 12:29:01 +00:00
< / 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/yannespositocomfr" >
s'abonner
< / 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" > Commentaires & Partage< / 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" > commentaires< / h2 >
< noscript >
Vous devez activer javascript pour commenter.
< / noscript >
< script type = "text/javascript" >
var idcomments_acct = 'a307f0044511ff1b5cfca573fc0a52e7';
2011-04-20 13:56:52 +00:00
var idcomments_post_id = '/Scratch/fr/blog/2010-03-23-Encapsulate-git/';
var idcomments_post_url = 'http://yannesposito.com/Scratch/fr/blog/2010-03-23-Encapsulate-git/';
2011-04-20 12:29:01 +00:00
< / script >
< span id = "IDCommentsPostTitle" style = "display:none" > < / span >
2011-04-20 13:56:52 +00:00
< 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" >
2011-04-20 13:56:52 +00:00
< ul > < li > < a href = "/Scratch/fr/" > Bienvenue< / a > < / li >
< li > < a href = "/Scratch/fr/blog/" > Blog< / a > < / li >
< li > < a href = "/Scratch/fr/softwares/" > Softwares< / a > < / li >
< li > < a href = "/Scratch/fr/about/" > À propos< / 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" >
articles précédents
< div class = "previous_article" >
2011-04-20 13:56:52 +00:00
< a href = "/Scratch/fr/blog/2010-03-22-Git-Tips/" > < span class = "nicer" > «< / span > Astuces Git< / a >
2011-04-20 12:29:01 +00:00
< / div >
< div class = "previous_article" >
2011-04-20 13:56:52 +00:00
< a href = "/Scratch/fr/blog/2010-02-23-When-regexp-is-not-the-best-solution/" > < span class = "nicer" > «< / span > Quand se passer des expressions régulières ?< / a >
2011-04-20 12:29:01 +00:00
< / div >
< div class = "previous_article" >
2011-04-20 13:56:52 +00:00
< a href = "/Scratch/fr/blog/2010-02-18-split-a-file-by-keyword/" > < span class = "nicer" > «< / span > découper un fichier par mots clés< / a >
2011-04-20 12:29:01 +00:00
< / div >
< / div >
< div id = "next_articles" >
articles suivants
< div class = "next_article" >
2011-04-20 13:56:52 +00:00
< a href = "/Scratch/fr/blog/2010-05-17-at-least-this-blog-revive/" > Je reviens à la vie ! < span class = "nicer" > »< / span > < / a >
2011-04-20 12:29:01 +00:00
< / div >
< div class = "next_article" >
2011-04-20 13:56:52 +00:00
< a href = "/Scratch/fr/blog/2010-05-19-How-to-cut-HTML-and-repair-it/" > Comment réparer un XML coupé ? < span class = "nicer" > »< / span > < / a >
2011-04-20 12:29:01 +00:00
< / div >
< div class = "next_article" >
2011-04-20 13:56:52 +00:00
< a href = "/Scratch/fr/blog/2010-05-24-Trees--Pragmatism-and-Formalism/" > Arbres ; Pragmatisme et Formalisme < 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/deed.fr" > Droits de reproduction ©, Yann Esposito< / a >
< / div >
< div id = "lastmod" >
Écrit le : 23/03/2010
2012-05-02 15:43:56 +00:00
modifié le : 26/04/2012
2011-04-20 12:29:01 +00:00
< / div >
< div >
Site entièrement réalisé avec
< a href = "http://www.vim.org" > Vim< / a >
et
< a href = "http://nanoc.stoneship.org" > nanoc< / a >
< / div >
< / div >
< div class = "clear" > < / div >
< / div >
< / body >
< / html >