检查已删除文件的历史记录

发布于 2024-07-10 19:04:28 字数 216 浏览 8 评论 0原文

如果我在 Subversion 中删除文件,如何查看它的历史记录和内容? 如果我尝试对不存在的文件执行 svn cat 或 svn log ,它会抱怨该文件不存在。

另外,如果我想恢复该文件,我应该 svn add 将其恢复吗?

(我特别询问了 Subversion,但我也想听听 Bazaar、Mercurial 和 Git 如何处理这种情况。)

If I delete a file in Subversion, how can I look at it's history and contents? If I try to do svn cat or svn log on a nonexistent file, it complains that the file doesn't exist.

Also, if I wanted to resurrect the file, should I just svn add it back?

(I asked specifically about Subversion, but I'd also like to hear about how Bazaar, Mercurial, and Git handle this case, too.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

ゝ杯具 2024-07-17 19:04:29

仅使用 GUI 的解决方案:

如果您知道文件名,但不知道其最后版本号,甚至不知道其路径:

  1. 上执行“显示日志”
  2. 从 Repo 浏览器在根目录 “显示全部”(位于日志对话框的底部)
  3. 在“过滤器”文本框(位于日志对话框的顶部)中键入文件名,

这样将仅显示添加/修改/删除文件的修订版本。 这是您的文件历史记录。

请注意,如果通过删除其父文件夹之一来删除文件,则日志中不会有“已删除”条目(因此 mjy 的解决方案将不会出现“工作)。 在这种情况下,过滤日志中的最新条目将与其删除时的内容相对应。

A solution using only the GUI:

If you know the name of the file, but don't know its last revision number or even its path:

  1. From Repo Browser do a "Show log" on the root
  2. Hit "Show All" (at the bottom of the log dialog)
  3. Type the filename into the Filter textbox (at the top of the log dialog)

This will then show only those revisions where the file was added/modified/deleted. This is your history of the file.

Note that if the file was deleted by deleting one of its parent folders, it won't have a 'deleted' entry in the log (and so mjy's solution won't work). In this case, its most recent entry in the filtered log will correspond to its contents at deletion.

云仙小弟 2024-07-17 19:04:29
svn log -v | grep -B50 YourDeletedFileName

将为您提供路径和修订版本。 在 git 中(还检查重命名):

git log --diff-filter=DR --name-only | grep -B50 YourDeletedFileName
svn log -v | grep -B50 YourDeletedFileName

Will get you the path and revision. In git (also checks for renames):

git log --diff-filter=DR --name-only | grep -B50 YourDeletedFileName
过期以后 2024-07-17 19:04:29

如果您不知道已删除文件的路径,则可以在过于繁重的 svn log 命令中搜索

svn log --search <deleted_file_or_pattern> -v

该命令可能是就像没有搜索选项一样对服务器进行重击,但至少涉及的其余资源(包括您的眼球)会有所缓解,因为这会告诉您该文件在哪个版本中被删除。 然后,您可以按照其他提示进行操作(主要使用相同的 svn log 命令,但已经在定义的路径上)。

If you don't know the path to the deleted file, turns out you can search for it in the otherwise all-too-heavy svn log command:

svn log --search <deleted_file_or_pattern> -v

The command is probably hammering the server just as much as it would without the search option, but at least the rest of involved resources (including your eyeballs) would be kinda relieved, since that will tell you in which revision that file was deleted. Then, you can follow the other tips (mainly using the same svn log command, but already on a defined path).

黯然#的苍凉 2024-07-17 19:04:29

除了达斯汀的回答之外,如果您只想检查内容,而不是检查它,在他的示例中您可以这样做:

$ git show 8d4a1f^:slosh.tac

: 分隔修订版和该修订版中的路径,有效地要求特定修订版的特定路径。

In addition to Dustin's answer, if you just want to examine the contents, and not check it out, in his example you can do:

$ git show 8d4a1f^:slosh.tac

the : separates a revision and a path in that revision, effectively asking for a specific path at a specific revision.

Bonjour°[大白 2024-07-17 19:04:29

使用此命令:

svn log -v | awk '/^r[0-9]+/ { rev = $1; }; / D .*filename_escaped_for_regex/ { print rev" "$2; };'

这将列出曾经删除与该模式匹配的任何文件的所有修订。
也就是说,如果您要搜索文件 README,则搜索所有 /src/README/src/README.first/some/deeply/将找到并列出隐藏/目录/READMENOT

如果您的文件名包含斜线(路径)、点或其他特殊正则表达式字符,请不要忘记对它们进行转义以避免不匹配或错误。

Use this command:

svn log -v | awk '/^r[0-9]+/ { rev = $1; }; / D .*filename_escaped_for_regex/ { print rev" "$2; };'

This will list all revisions that ever deleted any files matching the pattern.
That is, if you're searching for file README, then all of /src/README, /src/README.first, and /some/deeply/hidden/directory/READMENOT will be found and listed.

If your filename contains slashes (path), dots, or other special regex characters, don't forget to escape them to avoid mismatching or errors.

萝莉病 2024-07-17 19:04:29

发帖者在这里实际上问了3个问题:

  1. 如何在Subversion中查看已删除文件的历史记录?
  2. 如何在 Subversion 中查看已删除文件的内容?
  3. 如何在 Subversion 中恢复已删除的文件?

我在这里看到的所有答案都是针对问题2和3的。

问题1的答案是:

svn log http://server/svn/project/file@1234

您仍然需要获取文件上次存在时的修订号,其他人在这里已经明确回答了这一点。

The poster has actually asked 3 questions here:

  1. How do I look at the history of a deleted file in Subversion?
  2. How do I look at the contents of a deleted file in Subversion?
  3. How do I resurrect a deleted file in Subversion?

All the answers I see here are for questions 2 and 3.

The answer to question 1 is:

svn log http://server/svn/project/file@1234

You still need to get the revision number for when the file last existed, which is clearly answered by others here.

我不会写诗 2024-07-17 19:04:29

啊,自从我学习使用Bazaar以来,这是我尝试过的。 如果不成功,您似乎无法日志和注释当前已删除的文件...:-(

尝试过:

> bzr log -r 3 Stuff/ErrorParser.hta
bzr: ERROR: Path does not have any revision history: Stuff/ErrorParser.hta

但奇怪的是(幸运的是)我可以这样做:

> bzr cat -r 3 Stuff/ErrorParser.hta

并且:

> bzr diff -r 2..3 Stuff/ErrorParser.hta

按照上面的错误中的建议:(

> bzr log -v | grep -B 1 ErrorParser

调整-B--before-context) 参数(根据需要)。

Ah, since I am learning to use Bazaar, it is something I tried. Without success, it appears you cannot log and annotate removed files currently... :-(

Tried:

> bzr log -r 3 Stuff/ErrorParser.hta
bzr: ERROR: Path does not have any revision history: Stuff/ErrorParser.hta

but curiously (and fortunately) I can do:

> bzr cat -r 3 Stuff/ErrorParser.hta

and:

> bzr diff -r 2..3 Stuff/ErrorParser.hta

and as suggested in the bug above:

> bzr log -v | grep -B 1 ErrorParser

(adjust -B (--before-context) parameter as needed).

眼中杀气 2024-07-17 19:04:29

假设您的文件被命名为 ~/src/a/b/c/deleted.file

cd ~/src/a/b/c  # to the directory where you do the svn rm or svn mv command
#cd ~/src   # if you forget the correct directory, just to the root of repository
svn log -v | grep -w -B 9 deleted.file | head  # head show first 10 lines

示例输出,在 r90440 找到它,

...
r90440 | user | 2017-02-03 11:55:09 +0800 (Fri, 03 Feb 2017) | 4 lines
Changed paths:
  M /src/a/b/c/foo
  M /src/a/b/c/bar
  D /src/a/b/c/deleted.file

将其复制回以前的版本(90439=90440-1)

svn cp URL_of_deleted.file@90439 .

Assume your file was named as ~/src/a/b/c/deleted.file

cd ~/src/a/b/c  # to the directory where you do the svn rm or svn mv command
#cd ~/src   # if you forget the correct directory, just to the root of repository
svn log -v | grep -w -B 9 deleted.file | head  # head show first 10 lines

sample output, found it at r90440

...
r90440 | user | 2017-02-03 11:55:09 +0800 (Fri, 03 Feb 2017) | 4 lines
Changed paths:
  M /src/a/b/c/foo
  M /src/a/b/c/bar
  D /src/a/b/c/deleted.file

copy it back to previous version(90439=90440-1)

svn cp URL_of_deleted.file@90439 .
不打扰别人 2024-07-17 19:04:29

您需要指定修订版本。

svn log -r <revision> <deleted file>

You would need to specify a revision.

svn log -r <revision> <deleted file>
甜宝宝 2024-07-17 19:04:29

如果您想在重命名文件之前查看文件的历史记录,请按照 这里有一条评论,您可以使用

git log --follow -- current_file_name

If you're wanting to look at the history of a file prior to it being renamed, then as mentioned in a comment here you can use

git log --follow -- current_file_name
栀子花开つ 2024-07-17 19:04:29

我自己想要一个答案。 尝试以下操作以仅输出 svn log 中的删除内容。

svn log --stop-on-copy --verbose [--limit <limit>] <repo Url> | \
awk '{ if ($0 ~ /^r[0-9]+/) rev = $0 }
  { if ($0 ~ /^ D /) { if (rev != "") { print rev; rev = "" }; print $0 } }'

这通过awk过滤日志输出。 awk 缓冲它找到的每个修订行,仅在找到删除记录时输出它。 每个修订仅输出一次,因此修订中的多个删除被分组在一起(如标准 svn log 输出)。

您可以指定 --limit 来减少返回的记录数量。 您还可以根据需要删除 --stop-on-copy

我知道有人抱怨解析整个日志的效率。 我认为这是比 grep 及其“广撒网”-B 选项更好的解决方案。 我不知道它是否更有效,但我想不出 svn log 的替代方案。 它与@Alexander Amelkin 的答案类似,但不需要特定的名称。 这也是我的第一个 awk 脚本,因此它可能是非常规的。

I wanted an answer, myself. Try the following to output only deletes from svn log.

svn log --stop-on-copy --verbose [--limit <limit>] <repo Url> | \
awk '{ if ($0 ~ /^r[0-9]+/) rev = $0 }
  { if ($0 ~ /^ D /) { if (rev != "") { print rev; rev = "" }; print $0 } }'

This filters the log output through awk. awk buffers each revision line it finds, outputting it only when a delete record is found. Each revision is only output once, so multiple deletes in a revision are grouped together (as in standard svn log output).

You can specify a --limit to reduce the amount of records returned. You may also remove the --stop-on-copy, as needed.

I know there are complaints about the efficiency of parsing the whole log. I think this is a better solution than grep and its "cast a wide net" -B option. I don't know if it is more efficient, but I can't think of an alternative to svn log. It's similar to @Alexander Amelkin's answer, but doesn't need a specific name. It's also my first awk script, so it might be unconventional.

丢了幸福的猪 2024-07-17 19:04:29

您可以使用二分搜索找到提供该文件的最新版本。
我为此创建了一个简单的 /bin/bash 脚本:

function svnFindLast(){
 # The URL of the file to be found
 local URL="$1"
 # The SVN revision number which the file appears in (any rev where the file DOES exist)
 local r="$2"
 local R
 for i in $(seq 1 "${#URL}")
  do
   echo "checkingURL:'${URL:0:$i}'" >&2
   R="$(svn info --show-item revision "${URL:0:$i}" 2>/dev/null)"
   echo "R=$R" >&2
   [ -z "$R" ] || break
 done
 [ "$R" ] || {
  echo "It seems '$URL' is not in a valid SVN repository!" >&2
  return -1
 }
 while [ "$r" -ne "$R" -a "$(($r + 1))" -ne "$R" ]
 do
  T="$(($(($R + $r)) / 2))"
  if svn log "${URL}@${T}" >/dev/null 2>&1
   then
    r="$T"
    echo "r=$r" >&2
   else
    R="$T"
    echo "R=$R" >&2
  fi
 done
 echo "$r"
}

You can find the last revision which provides the file using binary search.
I have created a simple /bin/bash script for this:

function svnFindLast(){
 # The URL of the file to be found
 local URL="$1"
 # The SVN revision number which the file appears in (any rev where the file DOES exist)
 local r="$2"
 local R
 for i in $(seq 1 "${#URL}")
  do
   echo "checkingURL:'${URL:0:$i}'" >&2
   R="$(svn info --show-item revision "${URL:0:$i}" 2>/dev/null)"
   echo "R=$R" >&2
   [ -z "$R" ] || break
 done
 [ "$R" ] || {
  echo "It seems '$URL' is not in a valid SVN repository!" >&2
  return -1
 }
 while [ "$r" -ne "$R" -a "$(($r + 1))" -ne "$R" ]
 do
  T="$(($(($R + $r)) / 2))"
  if svn log "${URL}@${T}" >/dev/null 2>&1
   then
    r="$T"
    echo "r=$r" >&2
   else
    R="$T"
    echo "R=$R" >&2
  fi
 done
 echo "$r"
}
段念尘 2024-07-17 19:04:29

我编写了一个 php 脚本,将所有存储库的 svn 日志复制到 mysql 数据库中。 我现在可以对我的评论或文件名进行全文搜索。

I wrote a php script that copies the svn log of all my repositories into a mysql database. I can now do full text searches on my comments or names of files.

乜一 2024-07-17 19:04:28

当您想查看旧文件时,您确实应该知道以下之间的区别:

svn cat http://server/svn/project/file -r 1234

svn cat http://server/svn/project/file@1234

第一个版本查看现在可用的路径 http://server/svn/project/file 并检索该文件,因为它是修订版 1234 中的版本。(因此,此语法在之后不起作用文件删除)。

第二种语法获取修订版中作为 http://server/svn/project/file 提供的文件1234. 所以这个语法确实适用于已删除的文件。

您甚至可以组合这些方法来检索修订版 2345 中可用的文件 http://server/svn/project/文件,但其内容与 1234 中的内容相同:

svn cat http://server/svn/project/file@2345 -r 1234

When you want to look at old files you really should know the difference between:

svn cat http://server/svn/project/file -r 1234

and

svn cat http://server/svn/project/file@1234

The first version looks at the path that is now available as http://server/svn/project/file and retrieves that file as it was in revision 1234. (So this syntax does not work after a file delete).

The second syntax gets the file that was available as http://server/svn/project/file in revision 1234. So this syntax DOES work on deleted files.

You can even combine these methods to retrieve a file that was available in revision 2345 as http://server/svn/project/file but with the contents as it had in 1234 with:

svn cat http://server/svn/project/file@2345 -r 1234
去了角落 2024-07-17 19:04:28

首先,找到文件被删除的版本号:

svn log -v > log.txt

然后在 log.txt(不是 SVN 大师,所以我不知道更好的方法)中查找包含 的行

D <deleted file>

,看看是哪个版本。 然后,与其他答案一样,使用以前的修订版恢复文件。

First, find the revision number where the file got deleted:

svn log -v > log.txt

Then look in log.txt (not an SVN guru, so I don't know a better way) for a line with

D <deleted file>

and see which revision that was. Then, as in the other answers, resurrect the file using the previous revision.

岁月静好 2024-07-17 19:04:28

要获取已删除文件的日志,请使用

svn log -r lastrevisionthefileexisted

如果您想恢复文件并保留其版本历史记录,请使用

svn copy url/of/file@lastrevisionthefileexisted -r lastrevisionthefileexisted path/to/workingcopy/file

如果您只想要文件内容但未版本化(例如,为了快速检查),请使用

svn cat url/of/file@lastrevisionthefileexisted -r latrevisionthefileexisted > file

在任何情况下,请勿使用'svn up' 恢复已删除的文件!

To get the log of a deleted file, use

svn log -r lastrevisionthefileexisted

If you want to resurrect the file and keep its version history, use

svn copy url/of/file@lastrevisionthefileexisted -r lastrevisionthefileexisted path/to/workingcopy/file

If you just want the file content but unversioned (e.g., for a quick inspection), use

svn cat url/of/file@lastrevisionthefileexisted -r latrevisionthefileexisted > file

In any case, DO NOT use 'svn up' to get a deleted file back!

泪痕残 2024-07-17 19:04:28

git 中并没有什么特别的。 如果您知道文件的名称,则可以通过日志找到删除它的更改:

git log -n 1 -- filename

然后您可以使用该提交来获取删除之前存在的文件。

git checkout [last_revision]^ filename

示例:

dhcp-120:/tmp/slosh 587% ls -l slosh.tac
ls: slosh.tac: No such file or directory
dhcp-120:/tmp/slosh 588% git log -n 1 -- slosh.tac
commit 8d4a1f1a94e4aa37c1cb9d329a140d08eec1b587
Author: Dustin Sallings <[email protected]>
Date:   Mon Dec 15 11:25:00 2008 -0800

    Get rid of a .conf and replace it with .tac.
dhcp-120:/tmp/slosh 589% git checkout 8d4a1f^ slosh.tac
dhcp-120:/tmp/slosh 590% ll slosh.tac
-rw-------  1 dustin  wheel  822 Dec 30 12:52 slosh.tac

请注意,这实际上并没有将文件放回到版本控制中。 它只是将文件以其最终状态存在到当前位置。 然后您可以添加它或只是检查它或从那时起进行任何操作。

It's nothing particularly special in git. If you know the name of the file, you can find out the change that removed it with log:

git log -n 1 -- filename

Then you can use that commit to get the file as it existed before the deletion.

git checkout [last_revision]^ filename

Example:

dhcp-120:/tmp/slosh 587% ls -l slosh.tac
ls: slosh.tac: No such file or directory
dhcp-120:/tmp/slosh 588% git log -n 1 -- slosh.tac
commit 8d4a1f1a94e4aa37c1cb9d329a140d08eec1b587
Author: Dustin Sallings <[email protected]>
Date:   Mon Dec 15 11:25:00 2008 -0800

    Get rid of a .conf and replace it with .tac.
dhcp-120:/tmp/slosh 589% git checkout 8d4a1f^ slosh.tac
dhcp-120:/tmp/slosh 590% ll slosh.tac
-rw-------  1 dustin  wheel  822 Dec 30 12:52 slosh.tac

Note that this does not actually put the file back in revision control. It simply drops the file as it existed in its final state into the current location. You can then add it or just inspect it or whatever from that point.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文