2011-09-27 90 views
3

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

  1. 由混帐克隆接收文件/拉

  2. 后收到脚本modifyes文件属性(创建/更新),提交日期为创建和最后文件提交日期更新。

任何想法怎么写呢(可能是另一种方式)?

+2

似乎有点像http://stackoverflow.com/questions/2179722/git-checking-out- old-file-with-original-create-modified-timestamps,althoug务必阅读http://stackoverflow.com/questions/1964470/whats-the-equivalent-of-use-commit-times-for-git – VonC

回答

2

既然你在Windows的时候,这条巨蟒脚本可能会有所帮助:为每个文件适用的最近的时间戳提交该文件被修改:

以下是真的脚本的纯粹版本。对于实际使用我强烈建议的更稳健版本上述之一:

#!/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 
0

线:

文件= line.split( '\ T')[ - 1]

应改为:

文件= os.path.normpath(line.split( '\ T')[ - 1])

因为如果你从Linux克隆库到Windows这将是不同的路径分隔符和条件if file in filelist西港岛线无法正常工作