简体   繁体   中英

Capturing git log output on Windows XP with Python

This snippet of code works on most Unix I have access to (Linux, Solaris, AIX) as well as on Windows 7/ Server 2008 R2 and up that I've used to get the output of git log:

from subprocess import Popen, PIPE
import platform

cmd = ["git", "--no-pager"]
if platform.system() == "Windows":
    cmd.append("--work-tree=/path/to/working/copy")
cmd.extend(["log", "--pretty=format:\"%cd %h\"", "--date=short", "--", "/path/to/working/copy/filename/to/check"])

proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
proc.wait()
f = proc.stdout
some_stuff = f.read()
f.close()

However, on Windows XP using msysgit I'm not getting any value to the some_stuff variable, which is weird since running the generated command works on the command line on Windows.

Any tips on what should I do here?

After doing some research, here's what I've found:

  1. On Windows, msysgit recommends the use of the wrapper script git.cmd instead of directly specifying git.exe .
  2. msysgit is especially sensitive of normalized path filenames, in particular the drive letter must be in uppercase, and that the case of the filenames must match how GetLongPathName would output.
  3. Using forward slashes as well as backward slashes as path separators seems to still work.

By testing the above-mentioned, it seems that:

  • There's no need really to specify the --work-tree option anymore
  • "Normalizing" pathnames by passing it to os.path.normpath() will not work, as it will assume case-insensitivity and alter the drive letter to lowercase.
  • Using the short pathname (the output of GetShortPathName ) will not work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM