在 Windows 上克隆后 Git 恢复文件日期创建

标签 git

我在 win server 2003 上有我的远程存储库,在 etalon 的克隆项目之后,所有文件创建日期都变成了克隆日期。这没关系,但我需要将文件的创建日期恢复为第一次文件提交的日期。 据我所知,有一些方法可以使用 post-* 脚本,例如 post-receive。 主要思想:

  1. 通过git clone/pull接收文件

  2. 接收后脚本根据创建的第一个文件提交日期和更新的最后一个文件提交日期修改文件属性(创建/更新)。

有什么写法的想法(可能是另一种方式)?

最佳答案

由于您在 Windows 中,此 python 脚本可能会有所帮助:对于每个文件,应用最近一次修改文件的提交的时间戳:

下面是该脚本的真正 基本版本。对于实际使用,我强烈建议使用上面更强大的版本之一:

#!/usr/bin/env python
# Bare-bones version. Current dir must be top-level of work tree.
# Usage: git-restore-mtime-bare [pathspecs...]
# By default update all files
# Example: to only update only the README and files in ./doc:
# git-restore-mtime-bare README doc

import subprocess, shlex
import sys, os.path

filelist = set()
for path in (sys.argv[1:] or [os.path.curdir]):
    if os.path.isfile(path) or os.path.islink(path):
        filelist.add(os.path.relpath(path))
    elif os.path.isdir(path):
        for root, subdirs, files in os.walk(path):
            if '.git' in subdirs:
                subdirs.remove('.git')
            for file in files:
                filelist.add(os.path.relpath(os.path.join(root, file)))

mtime = 0
gitobj = subprocess.Popen(shlex.split('git whatchanged --pretty=%at'),
                          stdout=subprocess.PIPE)
for line in gitobj.stdout:
    line = line.strip()
    if not line: continue

    if line.startswith(':'):
        file = line.split('\t')[-1]
        if file in filelist:
            filelist.remove(file)
            #print mtime, file
            os.utime(file, (mtime, mtime))
    else:
        mtime = long(line)

    # All files done?
    if not filelist:
        break

关于在 Windows 上克隆后 Git 恢复文件日期创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7570217/

相关文章:

git 命令 - 推送时过滤提交

javascript - 在客户端以 html 的形式在 javascript 中显示 Git diff

git - 将 Git 更新到 MacBook Pro Sierra 上的官方发行版

java - Jenkins - 如果 git 中没有更改,则不要运行构建步骤

multithreading - JGit:是否有线程安全的方式来添加和更新文件

git - 在 debian wheezy 上安装 git 和 gitlab

git - 将 Mercurial (Hg) 分支提交到 GitHub

git - 在 windows powershell 上使用 git

git merge 两个具有不同目录结构的分支

尽管 set-upstream-to 和 push.default,Git 仍推送 HEAD -> master