LuaSVN is a library that allows to use some Subversion (SVN) facilities inside Lua programs.
Subversion is an open source version control system that has becoming very popular, currently it is the version control system used in more than half of the open source projects according Cia.vc.
Subversion is used not only in source code versioning, but also in web pages versioning, in fact, this was the original motivation to develop LuaSVN.
LuaSVN was developed using the C API offered by Subversion and implements a basic set of functions which can be used in a Lua program. The functions available in LuaSVN are similar to the functions offered by the svn tool, but not all svn functions were implemented.
Subversion uses some functionalities from Apache Portable Runtime (APR) project, because APR offers a standard interface to underlying platform-specific implementations, so Subversion does not need to worry about some lower level questions. Because of this, the memory handling is done by a pool from APR library, which by turn is passed as an argument to functions of the Subversion's C API.
The function init_function does the job to set up the initial configuration. The code related to the specific behavior of a function then comes after.
This is a brief history of LuaSVN:
require "svn"
now.
This version of LuaSVN works with URLs (e.g., "file:///", "http://", etc) and with paths (e.g., "wc/", "/home/name/workingcopy", etc). It also handles correctly the extra slash in the end of an URL or a path.
Often a LuaSVN function also has an integer optional parameter revision indicating the number of the version which should be considered. If this value is not specified, or if it is nil, then a default value will be supplied. Most of the time this value will be the youngest version of the repository, in case of an URL, and the base working copy, in case of a path. In general, if a function has an optional parameter, a default value will be used if an argument was not supplied or if nil was supplied.
The standard behavior of a LuaSVN function when an error occurs is to call lua_error.
The first thing you should do, so you can use LuaSVN, is to import the library. This can
be done including require ("svn")
in your Lua program. The functions below
are in the version 0.2.6 of the LuaSVN API:
svn.add (path)
Schedules the working copy path for addition to the repository. It does not return anything.
Example:
svn.add ("file_or_dir")
svn.cat (path_or_url [, revision])
Gets the content of a file identified by path_or_url. If revision is not supplied or if it is nil, then the most recent version will be considered.
Example:
content = svn.cat ("http://luasvn.googlecode.com/svn/trunk/0.2/luasvn.c")
svn.checkout (url, dir [, revision])
Gets a working copy of url, at revision, putting the new working copy in directory dir. If revision is not supplied or if it is nil, then the most recent version will be considered. Returns the number of the revision actually checked out from the repository.
Example:
rev = svn.checkout ("http://luasvn.googlecode.com/svn/trunk/0.2/", "luasvn/")
svn.cleanup ([dir])
Implements the Subversion function svn_client_cleanup, that recursively cleans up a working copy directory dir, finishing any incomplete operations, removing lockfiles, etc. It does not return anything.
If dir is not supplied or if it is nil, the current directory will be considered.
Example:
svn.cleanup ("mydir")
svn.commit ([path [, message]])
Commits files or directories into repository. Returns the number of the new revision
of the repository or nil if no commit was performed. The default value to
path is the current directory, so svn.commmit()
has the same
meaning of svn.commit("")
and svn.commit (nil)
.
Examples:
rev = svn.commit ()
rev = svn.commit ("wc", "a log message")
svn.copy (src_path, dest_path [, revision [, message]])
Copies src_path to dest_path, a non-existent working copy path or repository. If src_path is an URL, revision indicates which version should be copied. If revision is not supplied or if it is nil, then the most recent version will be considered. Returns the number of the revision in dest_path if a commit was performed or nil otherwise.
Example:
rev = svn.copy ("file.c", "file:///home/sergio/newrepos/copy.c")
svn.delete (path_or_url [, message])
Deletes the file path_or_url. If a commit is performed, then retuns the new version of the repository, otherwise returns nil.
Example:
rev = svn.delete ("file:///home/sergio/myrepos/foo.c")
svn.diff ([path1 [,rev1 [,path2 [,rev2 [,outfile [,errfile]]]]]])
Performs a diff operation between path1 and path2, which can be either working-copy paths or URLs. The default value of path1 is the current directory. If path2 is not supplied, then it is assumed to be equal to path1. If rev1 is nil, its default value is the youngest version of the repository, if path1 is an URL, and the base revision of the working copy otherwise. If rev2 is nil, its default value is the youngest version of the repository, if path2 is an URL, and the current state of the working copy otherwise.
If outfile is absent or is equal to nil, then stdout will be considered. In the same way, stderr will be considered if errfile is not supplied of if it is nil. This function does not return anything.
Examples:
svn.diff ("file:///home/sergio/myrepos/foo.c", 11)
svn.diff ("wc/file1.txt")
svn.diff ("http://luasvn.googlecode.com/svn/trunk/0.2/luasvn.c", 20, nil, nil, "out.txt", "err.txt")
svn.import (path, url [, message])
Imports file or directory path into repository url. Returns the version of the repository or nil if the import was not performed. If path is nil, then the current directory will be considered.
Examples:
rev = svn.import ("mydir", "file:///home/sergio/myrepos/", "first import")
rev = svn.import (nil, "file:///tmp/repos")
svn.list ([path_or_url [, revision]])
Lists the directory entry indicated by path_or_url. Returns a table where a key is an entry and the associated value is the last revision in which the file was modified. If path_or_url is not supplied or if it is nil, the current directory will be considered.
If revision is not supplied of if it is nil, then the youngest version of the repository will be considered if path_or_url is an URL, otherwise the base working copy will be considered.
Example:
t = svn.list ("file:///home/sergio/myrepos/") for i, v in pairs (t) do print (i, v) end
svn.log ([path_or_url [, revision]])
Lists the directory entry indicated by path_or_url. Returns a table where a key is the number of a revision in which path_or_url was modified.
The value associated with a key is also a table. This table has three fields: date, indicating when that revision was commited; author, the author of the modification; and message, the log message associated with the revision.
The default value to path_or_url is the current directory. If revision is not supplied of if it is nil, then the youngest version of the repository will be considered if path_or_url is an URL, otherwise the base working copy will be considered.
Example:
t = svn.log ("wc/foo.c") for i, v in pairs (t) do print (i, v.author, v.date, v.message) end
svn.merge (path1, rev1, path2, rev2, wcpath)
Merge changes from path1 at rev1 to path2 at rev2 into the working-copy path wcpath.
path1 and path2 are either URLs that refer to entries in the repository, or paths to entries in the working copy. In case of rev1/rev2 be nil, the default value will be the youngest version of the repository if path1/path2 is an URL, and the base working copy otherwise. This function does not return anything.
Example:
svn.merge ("file:///tmp/repos1", nil, "file:///tmp/repos2/", 3, "wc")
svn.mkdir (path [, message])
Creates a directory in a repository or in a working copy. Returns the number of the new version of the repository, or nil if path is a working copy.
Example:
rev = svn.mkdir ("file:///tmp/repos1/newdir")
svn.move (src_path, dest_path [, message])
Moves src_path to dest_path, a non-existent working copy path or repository. src_path must be a file or directory under version control, or the URL of a versioned item in the repository.
Returns the number of the revision in dest_path if a commit was performed or nil otherwise.
Example:
rev = svn.move ("file:///home/sergio/repos/foo.c", "file:///home/sergio/repos/fuu.c")
svn.propget (path, propname [, revision])
Returns a table whose keys are the items that have the property propname and whose values are the corresponding property values for propname.
If revision is not supplied of if it is nil, then the youngest version of the repository will be considered if path is an URL, otherwise the base working copy will be considered. Currently, this function operates recursively.
Example:
t = svn.propget ("wc/", "color") for i, v in pairs (t) do print (i, v) end
svn.proplist ([path [, revision]])
Returns a table with the entries in path that have some regular property. path can be an URL or working copy. The keys of the table are the name of the entries and the associated value is a table, that contains all the properties of the entry and the associated value.
The default value to path is the current directory. If revision is not supplied of if it is nil, then the youngest version of the repository will be considered if path is an URL, otherwise the base working copy will be considered. This function operates recursively.
Example:
t = svn.proplist ("wc/") for i, v in pairs (t) do print (i) for i, v in pairs (v) do print (i, v) end end
svn.propset (path, propname, propval)
Sets propname to propval on path. If path is a directory, then propname will be set on recursively on path. If propval is nil the property will be deleted. This function does not return anything and works only on working copies.
Example:
svn.propset ("/tmp/repos/file.txt", "color", "blue")
repos_create (path)
Creates a repository whose path is path. If the repository already exists, it will occur an error. This function creates only local repositories and does not return anything.
Example:
svn.repos_create ("myrepos")
repos_delete (path)
Deletes a repository whose path is path. If the repository does not exist, it will occur an error. This function works only for local repositories and does not return anything.
Example:
svn.repos_delete ("/home/name/myrepos")
svn.revprop_get (url, propname [, revision])
Returns the value associated with property propname. This routine does not affect the working copy; it is a pure network operation that queries an unversioned property (e.g., svn:date) attached to a revision.
If revision is not supplied of if it is nil, then the youngest version of the repository will be considered.
Example:
v = svn.revprop_get ("http://luasvn.googlecode.com/svn/trunk/0.2/luasvn.c", "svn:date", 21)
svn.revprop_list (url [, revision])
Returns a table with the revision properties attached to revision in the repository represented by url. This function does not read a working copy and it only reads unversioned properties (e.g., svn:log) attached to a revision.
If revision is not supplied of if it is nil, then the youngest version of the repository will be considered.
Example:
t = svn.revprop_list ("file:///tmp/repos1/") for i, v in pairs (t) do print (i, v) end
svn.revprop_set (url, propname, propval [, revision])
Sets propname to propval on revision revision in the repository represented by url. Returns the number of the revision actually affected. Notice that unless the administrator creates a pre-revprop-change hook in the repository, this feature will fail.
If revision is not supplied of if it is nil, then the youngest version of the repository will be considered.
Example:
r = svn.revprop_set ("file:///tmp/repos/file.txt", "color", "blue")
svn.status ([path [, revision]])
Returns a table with the status of the working copy path. Uses revision
as the revision number when contacting the repository to get more information. The keys of
the table are items in path and the associated value of an item is a string
following the same format of the string returned by a svn status
command
(with the flags update and verbose set).
The default value of path is the current directory. If revision is not supplied of if it is nil, then the youngest version of the repository will be considered.
Example:
t = svn.status ("wc") for i, v in pairs (t) do print (i, v) end
svn.update ([path [, revision]])
Updates the working tree path to revision. Returns the number of the revision to which revision was resolved.
The default value to path is the current directory. If revision is not supplied of if it is nil, then the youngest version of the repository will be considered.
Examples:
r = svn.update ()
r = svn.update ("wc/", 12)
Click here to download LuaSVN from LuaForge.
LuaSVN needs development versions of Subversion and APR. On Ubuntu, if you have the right permissions, you can install them in the following way:
sudo apt-get install libsvn-dev
Now you need to edit the makefile to reflect the places where Subversion and APR are installed. After this, you can type make and copy svn.so to a place where your Lua installation can find it, a typical one is: /usr/local/lib/lua/5.1/.
If you can not install LuaSVN this way, please take a look at http://www.freewisdom.org/projects/sputnik/Building_LuaSVN or feel free to ask.
To be able to use the URLs with http protocol, you will also need libneon, this can be a little bit complicated, but, in resume, you do not need to worry if you are building LuaSVN using apt-get and you should get libneon-0.25.X if you are building LuaSVN by yourself. I will try to put a more step-by-step guide if needed.
LuaSVN was developed by Sérgio Medeiros and is free software. If you have any comments or doubts, please send an e-mail to the LuaSVN list or for me (smedeiros at inf dot puc-rio dot br).
Copyright © 2007 Sérgio Medeiros.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.