# snaewe/gitt forked from cbx33/gitt

Switch branches/tags
Nothing to show
Fetching contributors…
Cannot retrieve contributors at this time
214 lines (171 sloc) 12.7 KB
 % afterhours7.tex - Afer Hours Week 7 \chapter{After Hours Week 7} \section{Network Communicating''} The last week began by looking at some networking communication. In the After Hours section this week, we are going to look at two tools which are used for network communication. \subsection{Brewing a website - in an instant} The \emph{Instaweb} tool allows you to spawn a web service quickly and easily, using a web daemon of your choice. The \emph{Instaweb} tool actually uses \texttt{gitweb} which is a more permanent solution for obtaining the same functionality as \emph{Instaweb}. If you have never played with web services before it would be worth spending a few minutes understanding a little about how web daemons work. This tool is not available on the Windows platform, but it can be used on both MacOS and Linux. \emph{Instaweb} allows us to browse our repository from the comfort of a web browser. We also have another benefit to running this from a web server. We can allow people to access our repository to look around without giving them the ability to change anything or make any commits. Before we run \texttt{git instaweb}, we need to ensure that we have a web daemon available to us. \emph{Instaweb} automatically creates a configuration file for the web daemon of your choice, runs the daemon on a custom network port, and loads a browser automatically pointing to the URL of the web instance you have just configured. On our example machine, we have installed \index{lighttpd}lighttpd as our choice of web daemon. Once again it is advised to understand the implications of this before you do it. On Ubuntu, this can be installed by running \texttt{apt-get install lighttpd}. \begin{code} john@satsuki:~/coderepo$git instaweb john@satsuki:~/coderepo$ \end{code} The tool is invoked by running \texttt{git instaweb} and when started, you should be presented with a browser as pictured below. In our case, lighttpd has been installed, and so Git will use that as its daemon. Firefox is also the default browser on this machine and so this is the browser that Git will choose to display the web page in. If we wanted to use an alternative browser, we could have supplied an argument with \texttt{--browser}, like \texttt{--browser chromium} for example. \figuregith{12cm}{images/f-af7-d1.png}{Instaweb's default page} The first thing to notice is that the description of the repository is unhelpful. In Figure 1, it is shown as \texttt{Unnamed repository; edit this...}. It is easy to rectify this, by editing the \texttt{.git/description} file. We are going to use the \texttt{echo} command from Linux, as we have throughout the book. \begin{code} john@satsuki:~/coderepo$echo "Our test repository" > .git/description john@satsuki:~/coderepo$ \end{code} On refreshing the page, the description will be updated. The next thing to notice is the url. The screenshot shows our url to be \texttt{http://127.0.0.1:1234/}, where \texttt{127.0.0.1} is the local address of our Git machine and \texttt{1234} is the port. Before we start taking a look around the web interface, we should learn how to end the \emph{Instaweb} session. If we close the web browser, it does not end the \emph{Instaweb} process. In fact, we could load up firefox again, type in the URL \texttt{http://127.0.0.1:1234/} and return to the home page of our Git repository. To close the instance of \emph{Instaweb} we run the following; \begin{code} john@satsuki:~/coderepo$git instaweb --stop john@satsuki:~/coderepo$ \end{code} Now would be a good time to take a quick look at what running \texttt{git instaweb} has done to our repository. If we take a look inside the \texttt{.git} folder, we can see that there is a new folder called \texttt{gitweb}. This folder contains configuration and log files for the \emph{Instaweb} process. The file we are most interested in is \texttt{httpd.conf}. Looking at the beginning of this file we should see something similar to the following. \begin{code} server.document-root = "/usr/share/gitweb" server.port = 1234 server.modules = ( "mod_setenv", "mod_cgi" ) server.indexfiles = ( "gitweb.cgi" ) server.pid-file = "/home/john/coderepo/.git/pid" server.errorlog = "/home/john/coderepo/.git/gitweb/lighttpd/error.log" \end{code} Here we can see the beginning of the config file that Git has created for using with lighttpd. If we had other web daemons installed, such as apache, we could override the default of lighttpd by supplying the \texttt{--httpd apache2}. Notice the port number which as been defined as \texttt{1234}. Let us run up \texttt{git instaweb} again and see what other features the web interface offers. \figuregith{12cm}{images/f-af7-d2.png}{Instaweb's repository page (top)} \figuregith{12cm}{images/f-af7-d3.png}{Instaweb's repository page (bottom)} Figures 2 and 3 show how the web interface looks when browsing our repository. At the top of the page we see a history of commits along with a search box which works similar to the \texttt{gitk} system which was discussed earlier. At the bottom of the page, we see our \textbf{tags} and \textbf{heads}. There are several link names which we will briefly describe now. \begin{itemize} \item \index{gitweb!shortlog}\textbf{shortlog} - Gives a log of the commits, similar to that shown in Figure 2. \item \index{gitweb!commit}\textbf{commit} - Returns a page that gives details about a specific commit. The \textbf{commit} page is shown in Figure 4. \item \index{gitweb!commitdiff}\textbf{commitdiff} - Shows how the chosen diff has changed since its parent. This is similar to running our \texttt{git diff HEAD~1..HEAD} command. \item \index{gitweb!tree}\textbf{tree} - This page is a simple listing of the tree object which shows all files present in that particular commit. \item \index{gitweb!snapshot}\textbf{snapshot} - Possibly one of the most useful links. Clicking on this will initiate a download of the repositories filesystem at that particular point in time. \item \index{gitweb!log}\textbf{log} - Gives a listing of the full log messages. \end{itemize} Let us take a little look around the interface. Choosing the first commit in the list on the homepage and clicking on the \textbf{commit} link moves on to the commit page. Here we can see detailed information about the commit. \figuregith{12cm}{images/f-af7-d4.png}{Instaweb's default page} The \textbf{commit}, \textbf{tree} and \textbf{parent} object hashes are displayed here for reference. The \textbf{tree} and \textbf{parent} lines are clickable links which will take us to those relevant sections. We are going to choose the \textbf{tree} link. A screenshot of the resulting page is shown in Figure 5. \figuregith{12cm}{images/f-af7-d5.png}{Instaweb's default page} Going back and clicking on the snapshot link will initiate a download of the entire filesystem, at that point in the repository's life. As shown in Figure 6. \figuregith{10cm}{images/f-af7-d6.png}{Instaweb's default page} This has been a very brief tour around the \texttt{gitweb} system. Hopefully you can see that this is a very useful tool to add to our Git suite. If you are considering setting up a permanent web based view for your Git repository, you should not use \emph{Instaweb}. This tool is intended for quick and easy web access to a repository. There are plenty of guides and tutorials available which explain the installation and configuration of \texttt{gitweb}. \subsection{Pushing and pulling with a daemon} The \texttt{git instaweb} daemon is very useful indeed. However, one function that it does not provide is the ability to clone or fetch from the URL. Another tool which comes with the Git package is the \indexgit{daemon} utility and it is this tool which is going to give us this functionality. We spoke earlier about the GIT protocol and it just so happens that \texttt{git daemon} uses the GIT protocol for transferring data. Let us have a look at a simple example of using \texttt{git daemon}. \begin{code} john@satsuki:~$git daemon --base-path=/home/john/coderepo \end{code} Notice how this command has not exited. This is because the process is still running, waiting for communication. We can now log into another machine on the same network and clone the repository using our \texttt{git clone} command. We will supply the host name of the machine that we configured with \texttt{git daemon}, and a directory for us to clone into. Notice the presence of \texttt{git://} instead of \texttt{ssh://} as we used in our previous network cloning example. \begin{code} rob@mimi:~$ cd /tmp rob@mimi:/tmp$mkdir source rob@mimi:/tmp$ git clone git://satsuki/ source Cloning into source... fatal: The remote end hung up unexpectedly rob@mimi:/tmp$\end{code} That did not go as expected and resulted in a failed clone. If we look back at the source machine we can see something interesteing has appeared. \begin{code} john@satsuki:~$ git daemon --base-path=/home/john/coderepo [4687] '/home/john/coderepo/.git': repository not exported. john@satsuki:~$\end{code} This is probably one of the most common of all errors when dealing with \texttt{git daemon}. By default, Git tries to protect your repositories and will not allow them to be exported unless you explicitly tell it to. We have two ways of doing this. We can append the \texttt{--export-all} parameter to \texttt{git daemon}, which will allow exporting of all repositories which are under the path described in \texttt{--base-path}. The second method is to explicitly tell Git on a repository by repository, that we would like for it to be exported, or more accurately that we would like to opportunity to export it. We are going to do the latter of the two and we do this by creating a special file in the \texttt{.git} directory, called \texttt{git-daemon-export-ok}. To add this file, we are going to need to stop the daemon by pressing the \textbf{ctrl+c} key combination. Then we ran run the commands as shown below. \begin{code} john@satsuki:~$ touch coderepo/.git/git-daemon-export-ok john@satsuki:~$git daemon --base-path=/home/john/coderepo \end{code} Now we can go back to our second machine again and try to clone the repository as before. \begin{code} rob@mimi:/tmp$ git clone git://satsuki/ source Cloning into source... remote: Counting objects: 71, done. remote: Compressing objects: 100% (51/51), done. remote: Total 71 (delta 16), reused 0 (delta 0) Receiving objects: 100% (71/71), 6.47 KiB, done. Resolving deltas: 100% (16/16), done. rob@mimi:/tmp\$ \end{code} Et voila! Our repository has been cloned. As we exported the root of our repository using the \texttt{--base-path} parameter, we do not need to specify to the clone command which repository we are trying to clone. If you remember we did have some other repositories in the home folder and if we had exported \texttt{/home/john/}, instead of \texttt{/home/john/coderepo}, then we could have chosen any of the repositories that lay in the home folder by appending their names to the URL. An example of this would be \texttt{git://satsuki/coderepo/} or \texttt{git://satsuki/coderepo-cl/}. By default the \texttt{git daemon} tool only allows people to fetch objects from the repository. This is quite sensible because as you can see there is no authentication present with the GIT protocol at all. You \emph{can} enable pushing of objects to your repository using the GIT protocol, but this is only advised within a well trusted LAN environment. Otherwise you are giving whoever else is on your network the capability to push whatever they want into your repository, which as you can understand is not a good idea. We can run the daemon tool in the background by supplying the \texttt{--detach} parameter. However if you are unfamiliar to the Linux world, this requires some knowledge to stop the daemon again. Running the daemon detached will result in your shell returning to the prompt, seemingly not have executed anything. In fact the process has been moved to the background and no longer supports shell interaction. The benefit of this is that you do not have to keep a shell window open to run it in. The downside is that you can no longer end the process with the \textbf{ctrl+c} key combination. You can hopefully see that by using the \texttt{git daemon} tool, it is possible for us to allow other people on our network to have access to our repositories. Whilst the \texttt{git daemon} tool is very useful, it does have its disadvantages as stated, primarily in areas of version control. However, seeing how efficient the GIT protocol is compared to its other counterparts, it often makes a fantastic device for making a repoitory available to pull from. There are much more complicated configurations that can be performed with the daemon tool, but these are out of the scope of this chapter. The manual page for \texttt{git daemon} has some examples to get you started, and even goes into the areas of virtual hosting to allow multiple Git \emph{sites} to exist on one server.