git: 如果追踪被删除的代码是哪个commit删除的?

在使用 git blame 时,默认情况下它只能显示当前文件中每一行代码的最后修改信息。如果某段代码已经被删除,那么直接使用 git blame 是无法追踪到这些被删除代码的历史记录的。

可以通过以下方法来追踪已经被删除的代码:


1. 结合 git log 查找删除代码的提交

git log 可以帮助你查找包含特定代码片段的提交历史,包括删除操作。

示例命令:
git log -S "被删除的代码片段"
  • -S 参数会搜索提交历史中引入或删除了指定字符串的提交。
  • 如果你知道被删除代码的具体内容(比如变量名、函数名等),可以直接用 -S 来定位。
示例:

假设你有一段被删除的代码是 console.log("debugging"),可以运行:

git log -S "console.log(\"debugging\")"

这将列出所有与该代码相关的提交记录,包括删除它的提交。

root@u2204:~/xenomai/linux-dovetail# git log -S "extern int __close_fd(struct files_struct *files, unsigned int fd);"
commit 6d256a904cd700fb374f689697901686bf3e9bdd
Author: Eric W. Biederman <ebiederm@xmission.com>
Date:   Fri Nov 20 17:14:38 2020 -0600

    file: Rename __close_fd to close_fd and remove the files parameter

    [ Upstream commit 8760c909f54a82aaa6e76da19afe798a0c77c3c3 ]

    The function __close_fd was added to support binder[1].  Now that
    binder has been fixed to no longer need __close_fd[2] all calls
    to __close_fd pass current->files.

    Therefore transform the files parameter into a local variable
    initialized to current->files, and rename __close_fd to close_fd to
    reflect this change, and keep it in sync with the similar changes to
    __alloc_fd, and __fd_install.

    This removes the need for callers to care about the extra care that
    needs to be take if anything except current->files is passed, by
    limiting the callers to only operation on current->files.

    [1] 483ce1d4b8c3 ("take descriptor-related part of close() to file.c")
    [2] 44d8047f1d87 ("binder: use standard functions to allocate fds")
    Acked-by: Christian Brauner <christian.brauner@ubuntu.com>
    v1: https://lkml.kernel.org/r/20200817220425.9389-17-ebiederm@xmission.com
    Link: https://lkml.kernel.org/r/20201120231441.29911-21-ebiederm@xmission.com
    Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
    Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
    Signed-off-by: Sasha Levin <sashal@kernel.org>

2. 查看具体的删除位置

当你找到删除代码的提交后,可以通过 git show 查看该提交的详细更改。

示例命令:
git show <commit-hash>

这将显示该提交的完整差异,包括被删除的代码。

root@u2204:~/xenomai/linux-dovetail# git show 6d256a904cd700fb374f689697901686bf3e9bdd
commit 6d256a904cd700fb374f689697901686bf3e9bdd
Author: Eric W. Biederman <ebiederm@xmission.com>
Date:   Fri Nov 20 17:14:38 2020 -0600

    file: Rename __close_fd to close_fd and remove the files parameter

    [ Upstream commit 8760c909f54a82aaa6e76da19afe798a0c77c3c3 ]

    The function __close_fd was added to support binder[1].  Now that
    binder has been fixed to no longer need __close_fd[2] all calls
    to __close_fd pass current->files.

    Therefore transform the files parameter into a local variable
    initialized to current->files, and rename __close_fd to close_fd to
    reflect this change, and keep it in sync with the similar changes to
    __alloc_fd, and __fd_install.

    This removes the need for callers to care about the extra care that
    needs to be take if anything except current->files is passed, by
    limiting the callers to only operation on current->files.

    [1] 483ce1d4b8c3 ("take descriptor-related part of close() to file.c")
    [2] 44d8047f1d87 ("binder: use standard functions to allocate fds")
    Acked-by: Christian Brauner <christian.brauner@ubuntu.com>
    v1: https://lkml.kernel.org/r/20200817220425.9389-17-ebiederm@xmission.com
    Link: https://lkml.kernel.org/r/20201120231441.29911-21-ebiederm@xmission.com
    Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
    Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
    Signed-off-by: Sasha Levin <sashal@kernel.org>

diff --git a/fs/file.c b/fs/file.c
index 48d0306e42cc..fdb84a64724b 100644
--- a/fs/file.c
+++ b/fs/file.c
@@ -659,11 +659,9 @@ static struct file *pick_file(struct files_struct *files, unsigned fd)
        return file;
 }

-/*
- * The same warnings as for __alloc_fd()/__fd_install() apply here...
- */
-int __close_fd(struct files_struct *files, unsigned fd)
+int close_fd(unsigned fd)
 {
+       struct files_struct *files = current->files;
        struct file *file;

        file = pick_file(files, fd);
@@ -672,7 +670,7 @@ int __close_fd(struct files_struct *files, unsigned fd)

        return filp_close(file, files);
 }
-EXPORT_SYMBOL(__close_fd); /* for ksys_close() */
+EXPORT_SYMBOL(close_fd); /* for ksys_close() */

 /**
  * __close_range() - Close all file descriptors in a given range.
@@ -1087,7 +1085,7 @@ int replace_fd(unsigned fd, struct file *file, unsigned flags)
        struct files_struct *files = current->files;

        if (!file)
-               return __close_fd(files, fd);
+               return close_fd(fd);

        if (fd >= rlimit(RLIMIT_NOFILE))
                return -EBADF;
diff --git a/fs/open.c b/fs/open.c
index 83f62cf1432c..48933cbb7539 100644
--- a/fs/open.c
+++ b/fs/open.c
@@ -1310,7 +1310,7 @@ EXPORT_SYMBOL(filp_close);
  */
 SYSCALL_DEFINE1(close, unsigned int, fd)
 {
-       int retval = __close_fd(current->files, fd);
+       int retval = close_fd(fd);

        /* can't restart close syscall because file table entry was cleared */
        if (unlikely(retval == -ERESTARTSYS ||
diff --git a/include/linux/fdtable.h b/include/linux/fdtable.h
index d26b884fcc5c..4ed3589f9294 100644
--- a/include/linux/fdtable.h
+++ b/include/linux/fdtable.h
@@ -124,8 +124,7 @@ int iterate_fd(struct files_struct *, unsigned,
                int (*)(const void *, struct file *, unsigned),
                const void *);

-extern int __close_fd(struct files_struct *files,
-                     unsigned int fd);
+extern int close_fd(unsigned int fd);
 extern int __close_range(unsigned int fd, unsigned int max_fd, unsigned int flags);
 extern int close_fd_get_file(unsigned int fd, struct file **res);
 extern int unshare_fd(unsigned long unshare_flags, unsigned int max_fds,
diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h
index 17a24e1180da..0bc3dd86f9e5 100644
--- a/include/linux/syscalls.h
+++ b/include/linux/syscalls.h
@@ -1320,16 +1320,16 @@ static inline long ksys_ftruncate(unsigned int fd, loff_t length)
        return do_sys_ftruncate(fd, length, 1);
 }

-extern int __close_fd(struct files_struct *files, unsigned int fd);
+extern int close_fd(unsigned int fd);

 /*
  * In contrast to sys_close(), this stub does not check whether the syscall
  * should or should not be restarted, but returns the raw error codes from
- * __close_fd().
+ * close_fd().
  */
 static inline int ksys_close(unsigned int fd)
 {
-       return __close_fd(current->files, fd);
+       return close_fd(fd);
 }

 extern long do_sys_truncate(const char __user *pathname, loff_t length);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值