Sending ascii characters through system or dos command

I'd like to send a system command to obtain the commit of a git tag, I'm trying to do this using the git rev-parse functionality:
cmd = 'git rev-parse v1.3^{}'
[resp,cmdOut] = system(cmd)
Gives me the response:
resp =
128
cmdOut =
'v1.3{}
fatal: ambiguous argument 'v1.3{}': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
'
The ^ character does not arrive in the shell. I tested this by:
cmd = 'echo git rev-parse v1.3^{}'
[resp,cmdOut] = system(cmd)
which gives me
resp =
0
cmdOut =
'git rev-parse v1.3{}
'
I'm on a windows computer. I tried the same yesterday in octave online (https://octave-online.net/) which does give me the expected respone. I do think that octave online runs linux though.

댓글 수: 5

try putting in \^ instead of ^
I tried that as well, gives me this:
cmd = 'echo git rev-parse v1.3\^{}'
[resp,cmdOut] = system(cmd)
resp =
0
cmdOut =
'git rev-parse v1.3\{}
'
Hmmm, maybe
cmd = 'echo git rev-parse v1.3^^{}'
(I am not using Windows so I cannot easily test myself.)
That works on my system (W8+R2020a), so feel free to move that to the answer section.
Works for me as well, thanks!

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2020년 9월 9일
Hmmm, maybe
cmd = 'echo git rev-parse v1.3^^{}'
(I am not using Windows so I cannot easily test myself.)

추가 답변 (1개)

Rik
Rik 2020년 9월 9일

0 개 추천

If you don't find a real solution: you can write the command to a bat file and run that instead. You can use the > symbol to redirect the output to a plain text file, which you can then read back.
You might want to use tempname to generate file names in the temp folder of the system running your function. That way you have the best chance of write access.

댓글 수: 2

I did find a solution by doing this:
function [tag,currentCommit] = getGitTag(folder)
%GETGITTAG Gets the git tag and commit for a given folder. When the current
%commit has no tag, the commit hash is also the tag.
if ~isfolder(folder)
error('%s does not exist',folder)
end
% Save the startdir
startDir = pwd;
% Go to folder
cd(folder)
% Get the current commit:
[resp,cmdOut] = system('git rev-parse HEAD');
if resp==0
commitList = split(cmdOut,newline);
% Remove the empty commits:
commitList(cellfun(@isempty,commitList)) = [];
% Select the first one:
currentCommit = commitList{1};
else
error('Could not get current commit because %s', cmdOut)
end
% Get the tag:
[resp,cmdOut] = system(['git tag --points-at ' currentCommit]);
if resp==0
% Split the tags into a list:
tagList = split(cmdOut,newline);
% Remove the empty tags:
tagList(cellfun(@isempty,tagList)) = [];
if ~isempty(tagList)
% Assign the tag when it is there:
tag = tagList{1};
else
% Otherwise assign the commit hash:
warning('Using commit %s as a tag because the current commit has no tag',currentCommit);
tag = currentCommit;
end
else
error('Could not get current commit because %s', cmdOut)
end
% Go back to the start dir:
cd(startDir)
end
In this way i turn the problem arround and I can avoid the "weird" character. I'm just wondering why I cannot pass a ^ into the shell..
Maybe you need to escape it in some way. I'm on mobile so I can't test it for you.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Source Control에 대해 자세히 알아보기

제품

릴리스

R2018b

태그

질문:

2020년 9월 9일

답변:

2020년 9월 9일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by