# LuaSVN - Using Subversion from Lua

lua svn lua subversion lua

### Introduction
Basic Operation
Change Log
API
Getting LuaSVN
Building LuaSVN
Contact and Further Information
License

## Introduction

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.

## Basic Operation

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.

## Change Log

This is a brief history of LuaSVN:

- 0.4.0: Released 16 Jun 2009
  - LuaSVN now compiles on Windows too! Thanks to Scott Weeks.
  - Error handling improved. Thanks again to Scott Weeks.
  - No changes in the API.
- 0.3.0: Released 14 Mar 2008
  - The main change was the addition of a configuration table
- 0.2.6: Released 24 Oct 2007
  - all the parameters of cleanup, diff and status are now optional.
  - some flags in diff, log, merge and revprop_set were changed to match the default values of the svn command line tool.
  - the makefile was improved.
- 0.2.5: Released 12 Oct 2007
  - To use LuaSVN you should do `require "svn"` now.
  - commit, list, log, proplist and update now assume the current directory as a default value to path.
  - commit, copy, delete, import, mkdir and move got an extra parameter to indicate a log message.
  - list now adds a slash to the name of an entry if that entry is a directory. The current value associated with an entry is the last revision in which that entry was modified.
  - Some functions were not working properly when trying to access a repository that has changed its directory structure, this is fixed now.
  - The table returned by proplist was wrong, please read the documentation of proplist to see the new return value.
  - If you supply 0 as the revision parameter of a function then the initial repository will be considered. In previous versions, supplying a 0 had the same meaning of supplying a nil.
  - The second parameter of diff is now optional too.
  - I think some libraries were not necessary during linking, so I removed them from the makefile. I would like to receive some feedback about the installation of LuaSVN.
- 0.2.0: Released 01 Oct 2007
  - Based on the libsvn_client library.
- 0.1.0: First version

## API

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.

Some LuaSVN functions also have as an optional parameter a configuration table, named config. This table has several fields that receive boolean values. Each function indicates which fields of config are important for it.

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.3.0 of the LuaSVN API:

- `svn.add (path [, config])`

  Schedules the working copy path for addition to the repository. It does not return anything.

  The following fields of config are important for this function:

  - recursive: default value is true
  - force: default value is false
  - no_ignore: default value is false

  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 [, config]])`

  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.

  The following fields of config are important for this function:

  - recursive: default value is true
  - ignore_externals: default value is false

  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 lock files, 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 [, config]]])`

  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.commit()` has the same meaning of `svn.commit("")` and `svn.commit (nil)`.

  The following fields of config are important for this function:

  - recursive: default value is true
  - keep_locks: default value is false

  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 [, config]])`

  Deletes the file path_or_url. If a commit is performed, then returns the new version of the repository, otherwise returns nil.

  The following field of config is important for this function:

  - force: default value is false

  Example:
  `rev = svn.delete ("file:///home/sergio/myrepos/foo.c")`

- `svn.diff ([path1 [,rev1 [,path2 [,rev2 [,outfile [,errfile [,config]]]]]]])`

  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 or if it is nil. This function does not return anything.

  The following fields of config are important for this function:

  - recursive: default value is true
  - ignore_ancestry: default value is false
  - no_diff_deleted: default value is false
  - force: default value is false

  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 [, config]])`

  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.

  The following fields of config are important for this function:

  - recursive: default value is true
  - no_ignore: default value is false

  Examples:
  `rev = svn.import ("mydir", "file:///home/sergio/myrepos/", "first import")`
  `rev = svn.import (nil, "file:///tmp/repos")`

- `svn.list ([path_or_url [, revision [, config]]])`

  Lists the entries of the directory indicated by path_or_url. Returns a table where a key is the name of a directory entry and the associated value is a table. This table has four fields:

  - author: the author of the last modification in that entry, or nil if the author is unknown
  - date: a string representing when that entry was committed
  - revision: the number of the last revision in which the entry was modified
  - size: the size of the entry, or nil if the entry is a directory

  If path_or_url is not supplied or if it is nil, the current directory will be considered.

  If revision is not supplied or 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.

  The following fields of config are important for this function:

  - recursive: default value is false
  - fetch_locks: default value is false

  Example:

      t = svn.list ("file:///home/sergio/myrepos/")

      for name, prop in pairs (t) do
      	print (name)
      	for k, v in (prop) do
      		print (k, v)
      	end
      end

- `svn.log ([path_or_url [,start [,end [,limit [,config]]]]])`

  Returns log information associated with 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 a table which has three fields:

  - date: indicating when that revision was committed
  - author: the author of the revision
  - message: the log message associated with the revision

  The default value to path_or_url is the current directory. The parameter start indicates the first revision number the function should consider, if start is not supplied or if it is nil, then its value will be zero. In the same way, end indicates the last revision number the function should consider, and its default value is the youngest version of the repository if path_or_url is an URL, otherwise, the base working copy will be considered as the default value.

  You can also use limit to limit the number of entries of the table this function returns.

  The following fields of config are important for this function:

  - discover_changed_paths: default value is false
  - stop_on_copy: default value is false

  Example:

      t = svn.list ("file:///home/sergio/myrepos/")

      for name, prop in pairs (t) do
      	print (name)
      	for k, v in (prop) do
      		print (k, v)
      	end
      end

- `svn.merge (path1, rev1, path2, rev2, wcpath [, config])`

  Merges 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.

  The following fields of config are important for this function:

  - recursive: default value is true
  - ignore_ancestry: default value is false
  - force: default value is false
  - dry_run: default value is false

  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 [, config]])`

  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.

  The following field of config is important for this function:

  - force: default value is false

  Example:
  `rev = svn.move ("file:///home/sergio/repos/foo.c", "file:///home/sergio/repos/fuu.c")`

- `svn.propget (path, propname [, revision [, config]])`

  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 or 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.

  The following field of config is important for this function:

  - recursive: default value is false

  Example:

      t = svn.propget ("wc/", "color", nil, {recursive=true})
      for k, v in pairs (t) do
          print (k, v)
      end

- `svn.proplist ([path [, revision [, config]]])`

  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.

  The following field of config is important for this function:

  - recursive: default value is false

  Example:

      t = svn.proplist ("wc/", 3, {recursive=true})
      for k, v in pairs (t) do
          print (k)
          for k, v in pairs (v) do
              print (k, v)
          end
      end

- `svn.propset (path, propname, propval [, config])`

  Sets propname to propval on path. If propval is nil the property will be deleted. This function does not return anything and works only on working copies.

  The following fields of config are important for this function:

  - recursive: default value is false
  - force: default value is false

  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 or 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 or if it is nil, then the youngest version of the repository will be considered.

  Example:

      t = svn.revprop_list ("file:///tmp/repos1/")
      for k, v in pairs (t) do
          print (k, v)
      end

- `svn.revprop_set (url, propname, propval [, revision [, config]])`

  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 or if it is nil, then the youngest version of the repository will be considered.

  The following field of config is important for this function:

  - force: default value is false

  Example:
  `r = svn.revprop_set ("file:///tmp/repos/file.txt", "color", "blue")`

- `svn.status ([path [, revision [, config]]])`

  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.

  The default value of path is the current directory. If revision is not supplied or if it is nil, then the youngest version of the repository will be considered.

  The following fields of config are important for this function:

  - recursive: default value is true
  - verbose: default value is false
  - show_updates: default value is false
  - no_ignore: default value is false
  - ignore_externals: default value is false

  Example:

      t = svn.status ("wc")
      for k, v in pairs (t) do
          print (k, 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 or if it is nil, then the youngest version of the repository will be considered.

  The following fields of config are important for this function:

  - recursive: default value is true
  - ignore_externals: default value is false

  Examples:
  `r = svn.update ()`
  `r = svn.update ("wc/", 12)`

## Getting LuaSVN

Click here to download LuaSVN from LuaForge.

## Building LuaSVN

There are several ways to build LuaSVN. One easy way to install LuaSVN is by using LuaRocks. If you can not install LuaSVN using LuaRocks, it may be useful to take a look in this messageof the LuaRocks mailing list and the corresponding answer.

On Ubuntu, if you have the right permissions, you can do this:

1. Install libsvn-dev:

       	sudo apt-get install libsvn-dev

2. Type make
3. 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 use a tool like apt-get, then you need to install Subversion and APR. After this, you should edit the makefile and adjust the variables APR, APR_UTIL and SUBVERSION. You should also use the right definition of LDFLAGS, as explained in the makefile.

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.

## Contact and Further Information

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 Lua list or for me (smedeiros at inf dot puc-rio dot br).

## License

Copyright © 2007-2008 LabLua.

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.