我在 win server 2003 上有我的远程存储库,在 etalon 的克隆项目之后,所有文件创建日期都变成了克隆日期。这没关系,但我需要将文件的创建日期恢复为第一次文件提交的日期。 据我所知,有一些方法可以使用 post-* 脚本,例如 post-receive。 主要思想:
通过git clone/pull接收文件
接收后脚本根据创建的第一个文件提交日期和更新的最后一个文件提交日期修改文件属性(创建/更新)。
有什么写法的想法(可能是另一种方式)?
最佳答案
由于您在 Windows 中,此 python 脚本可能会有所帮助:对于每个文件,应用最近一次修改文件的提交的时间戳:
- Core functionality , 与 --help, 调试消息。可以在工作树中的任何地方运行
- Full-fledged beast ,有很多选择。支持任何存储库布局。
下面是该脚本的真正 基本版本。对于实际使用,我强烈建议使用上面更强大的版本之一:
#!/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/