Showing posts with label git. Show all posts
Showing posts with label git. Show all posts

Thursday, February 3, 2011

creating a gem

this whole post is useless but i already wrote it so i'm posting


this video goes over everything in this post, except for conventional directory and file layout and the bin/ directory. but i didn't find it until i was about to hit publish. oh wells.

scattered notes


it's been difficult to find a tutorial on building a gem. it seems as though many ways have come and gone, and not gone. bundler can create gems. gem can create gems if you set up the files correctly. jeweler. new gem. hoe. echoe... well bundler gem some_gem_name creates a skeleton project for you and makes it into a git repo for you so you can start off clean. that's right, it creates a skeleton and makes it a git repo. awesome. and it uses git to specify which files are included in the gem. check out some_gem_name/some_gem_name.gemspec after running that command. you'll see something like:
s.files         = `git ls-files`.split("\n")
s.test_files    = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables   =
`git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }

so it makes sense that bundler starts off the project under git control. this also forces me to think about what exactly is and isn't in git.

gem structure conventions


from reading through actionmailer and some other gems, i've found that the basic structure is to mirror ruby class and module nesting th directory structure. after running bundle gem some_gem_name a new directory called some_gem_name is created in the current directory. under that is Gemfile, some_gem_name.gemspec, lib/ some other stuff. under lib/ is some_gem_name/ and some_gem_name.rb. in some_gem_name.rb is a skeleton with a module named SomeGemName! i guess bundler just uses the convention of taking out underscores and capitalizing the next letter, and capitilizing the first letter too. so that's cool.

so now we're in lib/some_gem_name/


in lib/some_gem_name/ is a file named version.rb. in that is simply:
module SomeGemName
VERSION = "0.0.0"
end

based on what i've read in actionmailer and thor, this is also where the meat goes. the guts of the code that make the gem do what it does. every file that does something important and lib like should go in here. and it should begin with some requires, and all the code should be between a [module|class] SomeGemName and an end. in addition, any directory under this one (for example lib/some_gem_name/foo_bar) should be the name of a module or class, with the first capital letter made lower, and every following capital made lower and preceded by an underscore. so the directory foo_bar corresponds to the class or module FooBar. furthermore, the nesting of those classes and modules in ruby should reflect the nesting of the directories on the filesystem. for example lib/some_gem_name/foo_bar/ should contain files that begin with some requires, followed by opening the class|module SomeGemName and then opening the class|module FooBar.
for example, inside lib/some_gem_name/foo_bar/foo_bar.rb, you would find something like:
require 'nokogiri'

module SomeGemName
class FooBar
def baz directance
fronlum = turboencabulate directance
fronlum.dipits
end
end
end

another convention seems to be having a name.rb as a sibling to every lib/**/name/. so there's a some_gem_name.rb corresponding to lib/some_gem_name/ and located at lib/some_gem_name.rb. another example, there should be a foo_bar.rb for lib/some_gem_name/foo_bar/ located at lib/some_gem_name/foo_bar.rb

bin/


there's also the option of creating a directory bin/ as a sibling to lib/. this is what bundler does. it looks like the convention is to use a file with no extension. that's how bundler creates the bundle command. so that's cool. within these files, you can just require 'some_gem_name' and use the stuff in your gem libs. the context is already set up so your some_gem_name

rake


right after running bundle gem some_gem_name && cd some_gem_name you can do rake -T to see a list that looks like this:
rake build    # Build template_validator-0.0.4.gem into the pkg directory
rake install  # Build and install template_validator-0.0.4.gem into system ...
rake release  # Create tag v0.0.4 and build and push template_validator-0.0...

so after writing the code into the locations described above, you can run rake build && rake install && irb -r some_gem_name to test out the gem you just created! so that's awesome.

Monday, April 5, 2010

gitweb serving gitosis repos via apache2 (on ubuntu karmic)

set up gitosis

install packages


sudo aptitude install gitweb apache2

configure gitweb


modify /etc/gitweb.conf:

# path to git projects (.git)
#$projectroot = "/var/cache/git";
$projectroot = "/srv/gitosis/repositories";

# directory to use for temp files
$git_temp = "/tmp";

# target of the home link on top of all pages
#$home_link = $my_uri || "/";

# html text to include at home page
$home_text = "indextext.html";

# file with project list; by default, simply scan the projectroot dir.
#$projects_list = $projectroot;
$projects_list = "/srv/gitosis/gitosis/projects.list";

# stylesheet to use
#$stylesheet = "/gitweb.css";
$stylesheet = "/git/gitweb.css";

# logo to use
#$logo = "/git-logo.png";
$logo = "/git/git-logo.png";

# the 'favicon'
#$favicon = "/git-favicon.png";
$favicon = "/git/git-favicon.png";

configure apache2


create /etc/apache2/conf.d/git with this content:

<Directory /var/www/git>
Allow from all
AllowOverride all
Order allow,deny
Options ExecCGI
<Files gitweb.cgi>
SetHandler cgi-script
</Files>
</Directory>
DirectoryIndex gitweb.cgi
SetEnv GITWEB_CONFIG /etc/gitweb.conf

make files visible to apache2

sudo mkdir /var/www/git
sudo cp /usr/share/gitweb/* /var/www/git
sudo cp /usr/lib/cgi-bin/gitweb.cgi /var/www/git
sudo /etc/init.d/apache2 reload
chmod 0755 /srv/gitosis/repositories/test_repo.git

configure gitosis



git clone gitosis@localhost:gitosis-admin.git
cd gitosis-admin

modify gitosis.conf, add the following:

[repo test_repo]
gitweb = yes
description = one line test repo description
owner = first_name last_name

save the configuration and push it back to gitosis

git add gitosis.conf
git ci -m "publishing test_repo via gitweb"
git push origin master

try it out


open http://localhost/git and see test_repo.git in column titled "Project"

references


Wednesday, March 3, 2010

git submodule moving

how do you update a git submodule? say the submodule used to be at gitosis@example.com:submodule_a.git, but the developers switched hosts and now it's at gitosis@example.net:submodule_a.git. turns out all you have to do is change .git/config.

in superproject/.git/config


before:
[submodule "submodule_a"]
url = gitosis@example.com:submodule_a.git

after:
[submodule "submodule_a"]
url = gitosis@example.net:submodule_a.git


now git submodule update will use example.net instead of example.com. but that doesn't change the content of the repository, just the configuration for your local copy of the superproject. if someone clones your repo, they'll try to reach the submodule at gitosis@example.com:submodule_a.git. to avoid this, update .gitmodules to point at the new location as well.

in superproject/.gitmodules


before:
[submodule "submodule_a"]
path = submodule_a
url = gitosis@example.com:submodule_a.git

after:
[submodule "submodule_a"]
path = submodule_a
url = gitosis@example.net:submodule_a.git

when git submodule init reads the .gitmodules file (in a newly cloned repo), it will get the new location for the submodule and setup the .git/config file accordingly.

helpful, but not what i wanted


places i went looking for this info, but didn't find it:
http://book.git-scm.com/5_submodules.html
http://progit.org/book/ch6-6.html
http://pitupepito.homelinux.org/?p=24
http://gaarai.com/2009/04/20/git-submodules-adding-using-removing-and-updating/
http://www.hackido.com/2009/01/quick-tip-remove-git-submodule.html

Tuesday, March 2, 2010

gitosis on ubuntu karmic 9.10

quickstart


as some_user@example.com:


on the server that will host the git repos,

sudo aptitude install gitosis
sudo -H -u gitosis gitosis-init < ~/.ssh/id_rsa.pub
git clone gitosis@localhost:gitosis-admin.git
cd gitosis-admin
scp newuser@example.net:.ssh/id_rsa.pub keydir/newuser@example.net.pub
vim gitosis.conf #:tabnew /usr/share/doc/gitosis/examples/example.conf
#add newuser@example.net to project_foo_group, make project_foo writable, close
git add .
git ci -m "add newuser@example.net to project_foo_group, make project_foo writable"
git push origin master

example gitosis.conf pushed back to gitosis-admin.git:

[gitosis]

[group gitosis-admin]
writable = gitosis-admin
members = some_user@example.com

[group project_foo_group]
members = some_user@example.com new_user@example.net
writable = project_foo

then as newuser at example.net:



cd project_foo
git remote add origin gitosis@example.com:project_foo.git
git push origin master

again, but cluttered with explanations


as some_user@example.com


sudo aptitude install gitosis
turns out there's a gitosis package in ubuntu. but it was unclear what installing it did. after reading some docs, such as /usr/share/doc/gitosis/README.rst.gz (installed with the gitosis package) and a blog post about hosting git, i was ready to guess. i found that the gitosis user had already been set up. finding its home directory was a matter of sudo su gitosis -;cd;pwd. this let me know first, that there was indeed a user named gitosis and second, that its home directory was at /srv/gitosis. this turns out to be irrelevant because the gitosis package already knows this. that directory was empty, iirc. so i guessed that i needed to pick up at gitosis-init.

sudo -H -u gitosis gitosis-init < ~/.ssh/id_rsa.pub

tell gitosis whom to trust for its administration. make the user gitosis run gitosis-init and give it the public key of whomever will admin the gitosis server (in this case it's the public rsa key of the current user). gitosis-init takes care of putting this file in the right place with the right name(/srv/gitosis/repositories/gitosis-admin.git/gitosis-export/keydir/some_user@example.com.pub). the exact place doesn't matter. it just matters that gitosis knows about it and will refer to it to verify requests, like cloning:

git clone gitosis@localhost:gitosis-admin.git

the current user (some_user@example.com) wants to clone from the user gitosis at localhost the gitosis-admin git repository. the user gitosis sees this request and needs to verify that it should fulfill that request. it uses the key (some_user@example.com.pub) that was just set up in the gitosis-init step. ssh magic happens here. the user gitosis trusts the incoming request and pushes out the repository to the requester, some_user@example.com. now some_user@example.com can modify the repository and write it back (all of these permissions and configurations are set up by gitosis-init.

cd gitosis-admin

when you clone the repo, its contents are put into a folder in the current directory, so go into it.

scp newuser@example.net:.ssh/id_rsa.pub keydir/newuser@example.net.pub

copy yet another public key into the key directory. this is the public key of newuser at example.net. copy it to the directory keydir with the filename user@host.pub. gitosis knows to look in this directory for files named like this for the public key.

vim gitosis.conf #:tabnew /usr/share/doc/gitosis/examples/example.conf
#add newuser@example.net to project_foo_group, make project_foo writable, close

modify gitosis.conf to add the relevant sections. scroll up a bit to see gitosis.conf. there is an example configuration at the location specified. in my example i create a group called new_project_group for the repository new_project. it says that the users some_user@example.com and newuser@example.net can write (which implies read and write) to the project new_project.

git add .
git ci -m "add newuser@example.net to project_foo_group, make project_foo writable"

save those changes! the public key and the configuration change! very important. but remember, this is just saving locally. you haven't told anyone else about this.

git push origin master

put those changes back up to the repository that gitosis@example.com knows about! gitosis will check its current configuration and see that it accepts changes from some_user@example.com to the gitosis-admin.git repository. gitosis has some kind of hook magic going on here. i'm not sure if it's a post-commit, or post-applypatch or what. but something black box happens here. the result of the black box is that changes you just pushed back to the gitosis-admin repo are now reflected by gitosis@example.com. it now knows about a new_project_group group, and the public key of newuser@example.net (for more ssh magic), and about the new_project repository.

as newuser@example.net


so now that the server has been set up and configured and doodadded, it's time to go put some content in it, other than the content that determines the configuration and the doodadding.

cd project_foo

newuser@example.net already has a project with some commits in it. so go into that directory.

git remote add origin gitosis@example.com:project_foo.git

tell that git repo about the new git server! gitosis on example.com knows about the project_foo git repo from the configuration file pushed to it earlier.

git push origin master

and the finish! this will push the master branch to the location nicknamed origin. origin is the nickname for gitosis@example.com:new_project.git. gitosis receives this request, does more ssh magic by looking at the public key pushed in gitosis-admin/keydir/newuser@example.net.pub. hooray! gitosis@example.com will see that the repo doesn't already exist, and accept the push, creating and populating the repo in one swoop! hooray!

Where am I?