Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

windows - Create archive of modified files in GIT via batch file

I'm using this git command to create an archive of the files modified within a specific commit:

git archive -o update.zip HEAD $(git diff --name-only COMMITID^)

where COMMITID is the id of the commit I'm looking to archive. This works fine from the command line but I would like to run it from a batch file. Here are the contents of the batch file I'm using:

git archive -o update.zip HEAD $(git diff --name-only %1^^)

where %1 is the commit id being passed in via SourceTree. The problem I'm having is that this command when run from a batch file returns the following error:

error: unknown option `name-only'

I'm guessing there may be some character escaping issues going but I'm unable to find what is specifically breaking.

How would I write that git command so that it will work from a batch file?

UPDATE

I tried removing the --name-only option and received the following error when trying the batch script via SourceTree:

fatal: path not found: $(git

Hopefully that helps narrow down what may be going on.

FURTHER UPDATE

Turns out my syntax was wrong for grabbing only the modified files from a specific commit but using msandiford's answer I came up with this batch file script that works perfectly:

setlocal enabledelayedexpansion
set output=
for /f "delims=" %%a in ('git diff-tree --no-commit-id --name-only -r %1^^') do ( set output=!output! "%%a" )
git archive -o update.zip HEAD %output%
endlocal
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Assuming you need a windows batch file, and not a bash script, here is a minimal batch file that may do what you want:

setlocal enabledelayedexpansion
set output=
for /f "delims=" %%a in ('git diff --name-only %1^^') do ( set output=!output! "%%a" )
git archive -o update.zip HEAD %output%
endlocal

It works by collecting all lines of the output of the git diff ... command into an environment variable, and then using this to perform the git archive ... operation.

Documentation for the Windows for command is here, including the /f switch. Documentation for the setlocal command is here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...