How can I find the root folder of a given subversion working copy

落爺英雄遲暮 提交于 2019-11-28 08:59:05

Here's my first go at an answer: I wrote a little bash script called svnbase:

#!/bin/bash -u

# this command outputs the top-most parent of the current folder that is still 
# under svn revision control to standard out

# if the current folder is not under svn revision control, nothing is output
# and a non-zero exit value is given

parent=""
grandparent="."

while [ -d "$grandparent/.svn" ]; do
    parent=$grandparent
    grandparent="$parent/.."
done

if [ ! -z "$parent" ]; then
    echo $parent
else
    exit 1
fi

So now I can do this:

[~/work/scripts/XXX/code/speech-detection/framework]$ cd $(svnbase)
[~/work/scripts/XXX]$ svn status
?      code/speech-detection/framework/output.old
[~/work/scripts/XXX]$ cd -
/home/XXXX/work/scripts/XXX/code/speech-detection/framework
[~/work/scripts/XXX/code/speech-detection/framework]$ 

If you use git-svn as your Subversion client, then you can interact transparently with a Subversion repository using Git commands locally. Git automatically shows you pending across the whole repository when you use commands like git status anywhere in the tree.

On Windows:

svn info . |findstr /C:"Working Copy Root Path:"

On Linux:

svn info . |grep -F "Working Copy Root Path:"

It can be assigned to a variable using a little string manipulation. Windows version:

FOR /F "delims=" %%i IN ('svn info . ^|findstr /C:"Working Copy Root Path:"') DO SET SOME_TEMP_VAR=%%i
SET WORKING_COPY_ROOT=%SOME_TEMP_VAR:~24%

On linux with svn, version 1.9.3 (r1718519), I run this: svn info --show-item wc-root

It is not possible. SVN can be checked out from any depth, and any subdir acts like brand new checkout.

edit: svnbase script in prev comment works, but it is not precise.

actually, if you look in your .svn/entries file, you'll find the base listed in there usually around line 6. In any event, the format of your svn url will give you some significant clues :P Oh yeah, i took the liberty of looking, and svn info will also tell you.

...sigh, that's not what you asked though. I like the solution in the first answer :P

Try the following command at any depth of your working copy:

svn info . --show-item wc-root --no-newline

Here is my solution, which is a hybrid of various solutions put here. It is written in PowerShell, as I needed it for a PowerShell module I am writing.

<#
    This command outputs the top-most parent of the current folder.

    If the current folder is not under svn revision control,
    it will recurse up one level, and try again.

    It stops at the root folder on the current drive.
    If the current folder's drive's root folder is not under svn revision control,
    it will throw an exception "No SVN Root Directory found."
#>
function Get-SvnRootDirectory {
    $currentDirectory = Get-Location

    do {
        $output = $(svn info $currentDirectory 2> $null)
        $output = $output | findstr /C:"Working Copy Root Path:"
        if ($LASTEXITCODE -eq 0)
        {
            break;
        }
        # if (Test-Path -PathType Container -Path $currentDirectory)
        # {
        #     throw "No SVN Root Directory found."
        # }
        $currentDirectory = Split-Path -Path $currentDirectory -Parent
        if ([string]::IsNullOrWhiteSpace($currentDirectory))
        {
            throw "No SVN Root Directory found."
        }
    }
    while ($true)

    $output -match "Working Copy Root Path: (?<Group>.*)$" | Out-Null
    return $Matches.Group
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!