Command parameters manipulation

English support forum

Moderators: white, Hacker, petermad, Stefan2

Post Reply
User avatar
jonathanpoulin
Junior Member
Junior Member
Posts: 28
Joined: 2020-12-19, 12:09 UTC
Location: Saint-Marc-des-Carrières, QC Canada
Contact:

Command parameters manipulation

Post by *jonathanpoulin »

Hi,

I would like to facilitate the use of TortoiseGit when I am using Total Commander.

Here is the use case. After a directories/files selection, I want to avoid to have to do a right-click on the panel, go to the TortoiseGit contextual menu and click on its right submenu (like 'Diff' or 'Show log'). I can use the keyboard as well, by doing Shift + F10 and navigating the contextual menu using the underlined letters and/or the arrow keys by example. But it would be quicker and easier to skip the contextual menu step. By example, if I want to commit modified file on 2 directories, I would select both of them with the Insert key then do a command to open directly the TortoiseGit' Commit dialog.

I know how to add a button in the button bar to call a program, in that case TortoiseGitProc.exe. I have to pass parameters to it. I know that there is special parameters like %P, %S, etc. The problem is that I don't know if there is a "built-in" way to get the selected directories/files with the following format (i.e. separated by asterisk). I don't want to have to call a .bat file for it.

Code: Select all

c:\path\to\my\dir1*c:\path\to\my\dir2
That's a requirement for the /path parameter of TortoiseGitProc.exe.
Since some of the commands can take a list of target paths (e.g. committing several specific files) the /path parameter can take several paths, separated by a * character.

https://tortoisegit.org/docs/tortoisegit/tgit-automation.html#tgit-automation-basics
Thank you
Jonathan
Thanks,
Jonathan Poulin
User avatar
ghisler(Author)
Site Admin
Site Admin
Posts: 48083
Joined: 2003-02-04, 09:46 UTC
Location: Switzerland
Contact:

Re: Command parameters manipulation

Post by *ghisler(Author) »

No, unfortunately %P%S separates multiple files by spaces. Maybe write a small EXE which converts the command line?
Author of Total Commander
https://www.ghisler.com
gdpr deleted 6
Power Member
Power Member
Posts: 872
Joined: 2013-09-04, 14:07 UTC

Re: Command parameters manipulation

Post by *gdpr deleted 6 »

Don't be afraid of scripting. Batch files are not the only "Windows-built-in" way to script tasks. Powershell to the rescue...

Define a toolbar button with the following command and parameter.

Command:

Code: Select all

powershell.exe -Command "&{$a=(((Get-Content $args[0]) -replace '[\\/]$') -join '*');Write-Host $a;Read-Host}"
Parameter:

Code: Select all

%UL
The command above is an example that shows you the result in a cmd window.
To make it usable, replace this part of the command:

Code: Select all

Write-Host $a;Read-Host
with whatever program you want to invoke. Keep the following in mind, because we are talking here about a Powershell scrip inlined in the command line:
- If the path of the executable and/or arguments need to be quoted, use single quotes (NOT double-quotes as you would normally do).
- The path of the executable needs to be prefixed by a & (ampersand) character. & is the "call" operator in Powershell. If the executable path is quoted, place the & in front outside the leading quote.
- $a is the variable in my script containing the selected file/dir paths with asterisks; use it as argument for the program you want to call where appropriate.
User avatar
jonathanpoulin
Junior Member
Junior Member
Posts: 28
Joined: 2020-12-19, 12:09 UTC
Location: Saint-Marc-des-Carrières, QC Canada
Contact:

Re: Command parameters manipulation

Post by *jonathanpoulin »

Thanks elgonzo for that PowerShell script! That's really nice! :D

In my case, for TortoiseGitProc.exe program, the parameter /path:"some\path" needs double quotes to work, based on my tests. With single quotes, it won't work. So I end up with the following command, in the case of a TortoiseGit Commit based on the TC selection.

Code: Select all

powershell.exe -WindowStyle Hidden -Command "&{$path=[string]::Format('\"{0}\"', (((Get-Content $args[0]) -replace '[\\/]$') -join '*'));&'C:\Program Files\TortoiseGit\bin\TortoiseGitProc.exe' /command:commit /path:$path}"
I don't know if there is a cleaner way to do this. If you know, please share. :P

Other than that, as you see, I tried to hide the PowerShell console that pops each time I invoke the TC command. It does not work with the -WindowStyle Hidden PowerShell' parameter, it still visible ~1s. I know however that I can use the "Run minimized" button option on the TC button bar. But this is not perfect since I can see the PowerShell taskbar button appearing... :roll:
Thanks,
Jonathan Poulin
User avatar
jonathanpoulin
Junior Member
Junior Member
Posts: 28
Joined: 2020-12-19, 12:09 UTC
Location: Saint-Marc-des-Carrières, QC Canada
Contact:

Re: Command parameters manipulation

Post by *jonathanpoulin »

Just a quick follow-up here. With the command as I shared on my previous post, I found that it is not working when I have no selection and the cursor is on the parent dir [..].

Any way to fix that?
Thanks,
Jonathan Poulin
User avatar
sqa_wizard
Power Member
Power Member
Posts: 3864
Joined: 2003-02-06, 11:41 UTC
Location: Germany

Re: Command parameters manipulation

Post by *sqa_wizard »

What should be achieved: Commit ".." ? ;-)
#5767 Personal license
User avatar
jonathanpoulin
Junior Member
Junior Member
Posts: 28
Joined: 2020-12-19, 12:09 UTC
Location: Saint-Marc-des-Carrières, QC Canada
Contact:

Re: Command parameters manipulation

Post by *jonathanpoulin »

sqa_wizard wrote: 2021-01-19, 22:55 UTC What should be achieved: Commit ".." ? ;-)
Yes, my point was not really logic here. I think that I need to manage it by myself in the PowerShell script. If the parameter returned by TC is empty, I have to pass parameter /path:. (with a dot which mean current directory) to TortoiseGitProc.exe.
Thanks,
Jonathan Poulin
User avatar
jonathanpoulin
Junior Member
Junior Member
Posts: 28
Joined: 2020-12-19, 12:09 UTC
Location: Saint-Marc-des-Carrières, QC Canada
Contact:

Re: Command parameters manipulation

Post by *jonathanpoulin »

ghisler(Author) wrote: 2021-01-18, 15:18 UTC No, unfortunately %P%S separates multiple files by spaces. Maybe write a small EXE which converts the command line?
Hi Christian! I happy to see that the author of Total Commander himself is answering to my questioning! :D

So for that case, as you have seen, I am using PowerShell to parse the parameter returned by TC. But I think this is something to consider to add more parameters for different scenarios like it. What do you think? Am I worng here? I think it would be nice to have one that fit my use case as-is. TortoiseGit is really popular.

Thank you for the great tool! :P
Jonathan
Thanks,
Jonathan Poulin
User avatar
jonathanpoulin
Junior Member
Junior Member
Posts: 28
Joined: 2020-12-19, 12:09 UTC
Location: Saint-Marc-des-Carrières, QC Canada
Contact:

Re: Command parameters manipulation

Post by *jonathanpoulin »

Hello again !

Here is below my latest version. Like that, if I have no selection and the cursor is on the parent dir [..], it's like if I had selected everything (i.e. will let me commit all files including those in subdirs). For that I pass the cur dir (using the dot, ".") if there's no selection.

Code: Select all

powershell.exe -Command "& { $path = '.'; if ($args[0]) { $path = '\"{0}\"' -f (((Get-Content $args[0]) -replace '[\\/]$') -join '*') }; & 'C:\Program Files\TortoiseGit\bin\TortoiseGitProc.exe' /command:commit /path:$path }"
But now I would like to ask you if there is an even better way to manage it. I saw that using %UL parameter will copy selection (actually copying files) to the temp dir each time. This is why I got a little delay using that method compared to the "traditional way" using the TortoiseGit right-click contextual-menu.

One more thing. There's some sort of side-effect. I am losing my selection after I close the Commit dialog. This can be annoying if I finally cancel my commit for some reason. I may want to keep the selection. Do you know why this is happening?

I will soon add a bunch of other TC commands that call the TortoiseGit commands. I think this will be really cool! Right now I have assigned the hotkey Shift + C for committing files! I have also added a TortoiseGit menu to the main menu containing the commands!

Thank you

EDIT: I double-checked and in fact TC is not copying files. I was totally wrong. So the slight delay I have is the call to PowerShell, and this is okay finally. Almost as fast as using the context-menu.
Thanks,
Jonathan Poulin
gdpr deleted 6
Power Member
Power Member
Posts: 872
Joined: 2013-09-04, 14:07 UTC

Re: Command parameters manipulation

Post by *gdpr deleted 6 »

jonathanpoulin wrote: 2021-01-20, 02:15 UTC Yes, my point was not really logic here. I think that I need to manage it by myself in the PowerShell script. If the parameter returned by TC is empty, I
have to pass parameter /path:. (with a dot which mean current directory) to TortoiseGitProc.exe.
You should be able to do this with another replacement, which can follow the first replacement operator, like:

Code: Select all

-replace '[\\/]$' -replace '^\.\.$', '.'
Please note that i haven't tried and verified this within a script inlined on the commandline. I still hope it works :)
User avatar
jonathanpoulin
Junior Member
Junior Member
Posts: 28
Joined: 2020-12-19, 12:09 UTC
Location: Saint-Marc-des-Carrières, QC Canada
Contact:

Re: Command parameters manipulation

Post by *jonathanpoulin »

elgonzo wrote: 2021-01-20, 07:55 UTCYou should be able to do this with another replacement, which can follow the first replacement operator, like:

Code: Select all

-replace '[\\/]$' -replace '^\.\.$', '.'
No, I tried it and it does not work in my case. But a big thanks for all your help. In fact I think the problem is that in this case parameter %UL point to the temp file which is empty. My solution I posted earlier the other day is working.
Thanks,
Jonathan Poulin
Post Reply