Thursday, January 10, 2013

Alfresco as git repository




How to use Alfresco as a Git Repository?

Git is a free & open source, Distributed Version Control and Source Code Management (SCM) System designed to handle everything from small to very large projects with speed and efficiency.  More info about Git: http://git-scm.com/about

GitHub is a web-based hosting service for software development projects that use the Git revision control system. It offers both paid plans for private repositories, and free accounts for open source projects.

GitHub’s public repository is free and accessible to the world, while private repository is paid and accessible to only those users to whom you want to share your project. More info on GitHub: https://github.com/

Here, we will use Alfresco as a private git repository, create projects, and share those projects to users (using Groups and Permissions).

For this, we have to configure CIFS in Alfresco.

Follow the below steps for configuring CIFS or refer http://wiki.alfresco.com/wiki/CIFS_Windows.

1) Add the following properties in /alfresco/tomcat/shared/classes/alfresco-global.properties file.

#------------------------------
# CIFS Configuration
#------------------------------

cifs.enabled=true
cifs.serverName=${localname}A

#Pointing Root Path to "Company_Home's children". By default it points to "Sites' children".
#protocols.rootPath=/${spaces.company_home.childname}/${spaces.sites.childname}
protocols.rootPath=/${spaces.company_home.childname}

2) Start Alfresco tomcat.

3) Map a network drive say (Z:\) on Windows with Alfresco.
    Drive: Z:
    Folder: \\DepakKeshwaniA\Alfresco
    Username: admin
    Password: admin

 


4) Now, Open a Git Bash Terminal on (Z:\) drive and execute the following commands.
Download and install Git for Windows from http://git-scm.com/downloads.

 

5) Create a folder in Alfresco say "SampleProject.git" that you want to make it as a software project. This project will act as a private remote git repository, which can be shared with users in Alfresco.

deepak.keswani@DEPAKKESHWANI /Z
$ mkdir GitRepo

deepak.keswani@DEPAKKESHWANI /Z
$ cd GitRepo

deepak.keswani@DEPAKKESHWANI /Z/GitRepo
$ mkdir SampleProject.git

deepak.keswani@DEPAKKESHWANI /Z/GitRepo
$ cd SampleProject.git/

deepak.keswani@DEPAKKESHWANI /Z/GitRepo/SampleProject.git
$ ls -ltr

-rwxr-xr-x    1 deepak.k Administ   393216 Jan  9 23:09 __ShowDetails.exe
-rwxr-xr-x    1 deepak.k Administ   393216 Jan  9 23:09 __CheckInOut.exe
-rw-r--r--    1 deepak.k Administ      131 Jan  9 23:09 __Alfresco.url

The above files are CIFS related files and not a part of Git Repository.

6) Now, we have to create a central repository using "git init --bare" command, which will be shared with all team members in your project. This will be a remote repository created on Alfresco.
More info on git init: http://git-scm.com/docs/git-init

deepak.keswani@DEPAKKESHWANI /Z/GitRepo/SampleProject.git
$ git init --bare
Initialized empty Git repository in z:/GitRepo/SampleProject.git/

You have now a remote git repository in place.
deepak.keswani@DEPAKKESHWANI /Z/GitRepo/SampleProject.git (BARE:master)
$ ls -ltr

-rwxr-xr-x    1 deepak.k Administ   393216 Jan  9 23:09 __ShowDetails.exe
-rwxr-xr-x    1 deepak.k Administ   393216 Jan  9 23:09 __CheckInOut.exe
-rw-r--r--    1 deepak.k Administ      131 Jan  9 23:09 __Alfresco.url
drwxr-xr-x    1 deepak.k Administ        0 Jan  9 23:18 refs
-rw-r--r--    1 deepak.k Administ       73 Jan  9 23:18 description
drwxr-xr-x    1 deepak.k Administ        0 Jan  9 23:19 hooks
drwxr-xr-x    1 deepak.k Administ        0 Jan  9 23:19 info
-rw-r--r--    1 deepak.k Administ       23 Jan  9 23:19 HEAD
-rw-r--r--    1 deepak.k Administ       81 Jan  9 23:19 config
drwxr-xr-x    1 deepak.k Administ        0 Jan  9 23:25 objects

Next, we will create one Software project on our local machine, commit that project into a local git repository, and finally add that newly created "remote" repository to our local git repository.

7) Open Git Bash Terminal on (E:\) drive and execute the following commands:

deepak.keswani@DEPAKKESHWANI /E
$ mkdir SampleProject

deepak.keswani@DEPAKKESHWANI /E
$ cd SampleProject/

8) Create an empty local git repository.
deepak.keswani@DEPAKKESHWANI /E/SampleProject
$ git init
Initialized empty Git repository in e:/SampleProject/.git/

The above command creates a hidden .git folder.

deepak.keswani@DEPAKKESHWANI /E/SampleProject
$ ls -altr

drwxr-xr-x   53 deepak.k Administ        0 Jan  9 23:20 ..
drwxr-xr-x    4 deepak.k Administ        0 Jan  9 23:23 .
drwxr-xr-x   12 deepak.k Administ        0 Jan  9 23:25 .git

You can check what configuration settings are there in .git/config file.

deepak.keswani@DEPAKKESHWANI /E/SampleProject (master)
$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true
        hideDotFiles = dotGitOnly

9) This step will add locally the previously created remote Git repository.
deepak.keswani@DEPAKKESHWANI /E/SampleProject (master)
$ git remote add origin file://Z:/GitRepo/SampleProject.git/

Git can transfer data between two repositories via file://, ssh://, git:// and https:// protocols.                 
Here, we will use local file:// protocol as we have mapped the Alfresco Repository on our local (Z:\) drive using CIFS.

More info about transfer protocols: http://git-scm.com/book/ch4-1.html
More info on git remote: http://git-scm.com/docs/git-remote
               
You can check what has been added to .git/config file.
deepak.keswani@DEPAKKESHWANI /E/SampleProject (master)
$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true
        hideDotFiles = dotGitOnly
[remote "origin"]
        url = file://z:/GitRepo/SampleProject.git/
        fetch = +refs/heads/*:refs/remotes/origin/*

Now, you can use the usual Git Lifecycle.

And can add, commit all your files locally, and "push to origin", which will push your code files to your central Git repository on Alfresco.

You can check which branch is active.
deepak.keswani@DEPAKKESHWANI /E/SampleProject (master)
$ git branch -a
* master

10) Create project folder structure and add some code/resource files to it.                         
deepak.keswani@DEPAKKESHWANI /E/SampleProject (master)
$ mkdir src

deepak.keswani@DEPAKKESHWANI /E/SampleProject (master)
$ cd src/

deepak.keswani@DEPAKKESHWANI /E/SampleProject/src (master)
$ cat > ReadMe.txt
This is a source folder. All your code files go here.

deepak.keswani@DEPAKKESHWANI /E/SampleProject/src (master)
$ ls -ltr
total 1
-rw-r--r--    1 deepak.k Administ       25 Jan  9 23:28 ReadMe.txt

deepak.keswani@DEPAKKESHWANI /E/SampleProject/src (master)
$ cd ..

11) Check the working tree status. It shows what all files need to be committed on local repository. http://git-scm.com/docs/git-status

deepak.keswani@DEPAKKESHWANI /E/SampleProject (master)
$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       src/
Nothing added to commit but untracked files present (use "git add" to track)
 
12) Adding file contents to the index. http://git-scm.com/docs/git-add

deepak.keswani@DEPAKKESHWANI /E/SampleProject (master)
$ git add .

13) Commit changes to the repository. http://git-scm.com/docs/git-commit

deepak.keswani@DEPAKKESHWANI /E/SampleProject (master)
$ git commit -m "readme file commit"
[master (root-commit) 2227d96] readme file commit
 1 file changed, 1 insertion(+)
 create mode 100644 src/ReadMe.txt


14) Push your local refs to remote refs. http://git-scm.com/docs/git-push

deepak.keswani@DEPAKKESHWANI /E/SampleProject (master)
$ git push origin master
Counting objects: 4, done.
Writing objects: 100% (4/4), 288 bytes, done.
Total 4 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (4/4), done.
To file://z:/GitRepo/SampleProject.git/
 * [new branch]      master -> master

NOTE: If you are logged in as a different user (not “admin”) through CIFS on Alfresco then, make sure you have Read/Write/Edit/Delete permissions on that project space in Alfresco.  Else you will get an error while pushing the code to remote repository.

deepak.keswani@DEPAKKESHWANI /E/SampleProject (master)
$ git push origin master
Counting objects: 6, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (5/5), 545 bytes, done.
Total 5 (delta 0), reused 0 (delta 0)
error: insufficient permission for adding an object to repository database ./objects

fatal: failed to write object
error: unpack failed: unpack-objects abnormal exit
To file://Z:/GitRepo/SampleProject.git
 ! [remote rejected] master -> master (n/a (unpacker error))
error: failed to push some refs to 'file://Z:/GitRepo/SampleProject.git'

You can check remote-tracking branches and local branches by following command.
More info on git branch: http://git-scm.com/docs/git-branch


deepak.keswani@DEPAKKESHWANI /E/SampleProject (master)
$ git branch -a
* master
  remotes/origin/master
   
Check the working tree status. http://git-scm.com/docs/git-status

deepak.keswani@DEPAKKESHWANI /E/SampleProject (master)
$ git status
# On branch master
nothing to commit (working directory clean)



In Association with Amazon.in

2 comments:

  1. An interesting concept. But, with both Alfresco and git having versions, isn't this a bit redundant? Ie. you change a file, so now you have a new version in git. But as you commit to Alfresco, you also get a new version of it in Alfresco?

    ReplyDelete
  2. Hi zlayer,

    First of all thank you so much for following this post. :)

    And that's a really good question. Git has its own version control, so we should disable versioning of Alfresco on GitRepo folder where we keep our Project code.

    Thanks


    ReplyDelete