Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 529
hereswhatidid Avatar asked Feb 07 '14 23:02

hereswhatidid


People also ask

What is the git command to create the archive files?

The git archive command is a Git command line utility that will create an archive file from specified Git Refs like, commits, branches, or trees.

How do you git add all modified files?

The easiest way to add all files to your Git repository is to use the “git add” command followed by the “-A” option for “all”. In this case, the new (or untracked), deleted and modified files will be added to your Git staging area.

How do you make a batch file that creates a batch file?

To create a Windows batch file, follow these steps: Open a text file, such as a Notepad or WordPad document. Add your commands, starting with @echo [off], followed by, each in a new line, title [title of your batch script], echo [first line], and pause. Save your file with the file extension BAT, for example, test.


1 Answers

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.

like image 156
clstrfsck Avatar answered Sep 17 '22 20:09

clstrfsck