1

In a Unix shell, I can get the parent command using ps -ocommand= -p $PPID. How can I do the same from the Windows shell?

I need this for a Git pre-commit hook that detects whether the commit was initiated with the --amend flag.

zh2l4d7s
  • 97
  • 1
  • 9
  • You might need to compile a binary to get what you need. Someone might have a powershell answer but this seems to be the most effective... http://stackoverflow.com/questions/185254/how-can-a-win32-process-get-the-pid-of-its-parent – Dave Apr 16 '15 at 23:16
  • I don’t think you can use the calling process as a safe indicator of whether something is an amend or not. Your solution will likely be not portable and highly dependent on some specific workflow—at which point you could just adjust that workflow instead. – poke Apr 16 '15 at 23:28
  • 1
    @Dave Unfortunately I don't think that's an option for me. I'm working on Windows support for [Overcommit](https://github.com/brigade/overcommit), so this needs to be something I can do in a Ruby script. Specifically, I'm trying to make [this method](https://github.com/brigade/overcommit/blob/v0.24.0/lib/overcommit/utils.rb#L123) compatible with Windows. – zh2l4d7s Apr 16 '15 at 23:30
  • @poke Yes, it's definitely flimsy, but I haven't been able to come up with a better way to [detect an amendment](https://github.com/brigade/overcommit/blob/v0.24.0/lib/overcommit/hook_context/pre_commit.rb#L12). Do you have any suggestions? – zh2l4d7s Apr 16 '15 at 23:34
  • Does [this question](http://stackoverflow.com/q/7486717/258523) help? – Etan Reisner Apr 16 '15 at 23:40
  • 1
    On Windows, this will be especially tricky since Git is executed throught a number of different programs (`git.exe` and several `sh.exe`). Depending on the version of Git for Windows, this is different too (the new Git for Windows which is no longer built by msysgit produces a different executable layout). – poke Apr 16 '15 at 23:45
  • @poke, are you sure that Git for Windows is no longer msysgit? The "maintained build" link on [the download page](http://git-scm.com/download/windows) still goes to https://msysgit.github.io/. – ChrisGPT was on strike Apr 17 '15 at 00:17
  • The team behind msysgit stopped working on msysgit quite a while ago and started with a new development environment based on MSYS/mingw. It took them quite a while (that’s why the “maintained build” of Git for Windows is an old 1.9.x) but they just released the first preview. It’s still Git for Windows, made by the same team, but with a different development chain and a different way the end product ends up—which is why I believe that you would run into additional issues there. – poke Apr 17 '15 at 00:22
  • Commit hooks are a silly idea in Git, since committing isn't publishing. "git commit" isn't "svn commit" or "cvs commit". Users who rewrite their set of patches seventeen times before pushing it out will be annoyed by hooks going off. And do you distinguish "git commit" from, say, "git cherry-pick?" A cherry pick also creates a new commit. – Kaz Apr 17 '15 at 05:28
  • 3
    @Kaz I'm not sure what you mean. Pre-commit hooks are a great tool for validating your changes before committing. Obviously they can be bypassed, and shouldn't be used to enforce policy (that should be the job of a server-side hook), but they are a great help in finding broken or sloppy code before it's committed. – zh2l4d7s Apr 17 '15 at 08:10

1 Answers1

0

A crude way of doing this is by looking up current PID using a title query.

title ABC
for /f "tokens=2" %%P in ('tasklist /V ^| findstr "ABC"') do set CurrentPid=%%P
for /f "tokens=2 skip=1" %%P in ('wmic process where ProcessId^=%CurrentPid% get Caption^,ParentProcessId^,ProcessId') do set ParentProcessId=%%P
wmic process where ProcessId=%ParentProcessId% get CommandLine

There is a lot that can be optimized there.

Parag Doke
  • 863
  • 7
  • 17