Thursday, May 3, 2012

GIT snippet #2

Aliases Very handfull way to make your console interaction easier. Here we go. Basic:
git config --global alias.go checkout
git config --global alias.st status
git config --global alias.gc commit
git config --global alias.br branch
git config --global alias.gr rebase
git config --global alias.gs stash
git config --global alias.l pull
git config --global alias.p push
Hardcore:
git config --global alias.grm rebase master
git config --global alias.god checkout dev
git config --global alias.gom checkout master
git config --global alias.gmd merge dev
So usual commit-push cycle would be:
git gc -a -m "master"
git gom // checkout master
git l   // pull
git god // checkout dev
git grm // rebase master
git gom // checkout master
git gmd // merge dev
git p   // push
git god // git checkout dev
Want more hardcore? We can write this to bash aliases, just add this lines to ~/.bashrc:
alias gl='git pull'
alias gp='git push'
alias gs='git status'
alias gc='git commit'
alias gcm='git commit -a -m'
alias go='git checkout'
alias gb='git branch'
alias gs='git status'
alias god='git checkout dev'
alias gom='git checkout master'
alias gmd='git merge dev'
alias grm='git rebase master'
alias grc='git rebase --continue'
alias gra='git rebase --abort'
So now our commit-push cycle would be:
gc -a -m "master"
gom // checkout master
gl   // pull
god // checkout dev
grm // rebase master
gom // checkout master
gmd // merge dev
gp   // push
god // git checkout dev
And if you want to automate rebase this action, you can write a bash script:
#!/bin/bash

branch=$1

cd ~/projects/$branch
git checkout dev
git stash
git checkout master
git pull
git checkout dev
git rebase master
if grep -q $commitString <<< $output; then
 git reset HEAD^
fi 
git stash apply
cd "`pwd`"
You can call this script by ./script.sh project_name This script will also rollback if you have rebase conflicts. Then better actually do such an action manually.

Wednesday, May 2, 2012

GIT Snippet

Just a tiny git snippet for those, who are not familiar with git. So here we go: Create repo
mkdir new_project
cd new_project
git init
touch README.MD
git add .
git commit -a -m "initial commit"
or clone existing repo for example - existing github repo
git clone git@github.com:username/some_existing_project.git
cd some_existing_project
if you want to keep track repo:
git remote add upstream git://github.com/username/some_existing_project.git
git fetch upstream
Create branch
git checkout -b dev
Now we have a special branch dev for development. After some changes are made, and some task is complete, we may want to merge them with original branch. Actually better use not usual merge command, but more powerfull rebase So: Commit and rebase
git add .
git commit -a -m "Commit message. Write some small one row title about the problem solved."
git checkout master
git pull
git checkout dev
git rebase master
Solve rebase conflicts If there are some conflicts, you should solve them by any merge-helper tool (IDE's usually have them), or manually. Then
git add .
git rebase --continue
or if you want to abort rebase
git rebase --abort
after succesfull rebase you may want to push changes to the origin gitrepo Push to origin
git checkout master
git merge dev
git push
git checkout dev
That's all. This is all basis that you need to work with git. More complex actions may appear, but they are not so often. Here are some links:

Mixing a trait dynamically. Copy a trait

Fantastically! I was dreaming about this for a long time. Imagine you have a trait, like
trait Dated {
 def createDate: Date
 def createAuthor: Author
}
Just some trait form db orm entity. What if you want just set nececery fields to this Dated instance, without any knowledge what case class is it, like:
def setEntityDate(entity: Dated) = entity.copy(createDate = /*now*/, createAuthor = /*currentUser*/)
This task can be accomplished by scala macroses, read about it here http://stackoverflow.com/questions/10373318/mixing-in-a-trait-dynamically and here https://gist.github.com/2559714