2011-09-27 44 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西港島線無法正常工作