Skip to content

Archive method does not support paths parameter #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
maurociancio opened this issue Jul 8, 2012 · 8 comments
Closed

Archive method does not support paths parameter #67

maurociancio opened this issue Jul 8, 2012 · 8 comments
Assignees

Comments

@maurociancio
Copy link

In the following code you are not able to supply what paths you would like to archive (and not all the repo).

repo = git.Repo("/git/repo")
repo.archive(ostream=temparchive, treeish="master", format="tar")

Meanwhile, if you use the git archive command you can suplly what paths you would like to archive.
An extra kw args could be the list of paths you want in the tar file.

@nuarhu
Copy link

nuarhu commented Jul 10, 2012

Me too...!

I've tried setting the "working_dir" but it's not used for the archive call - although this is what "git archive" actually does: if you are in a subdirectory, it will only archive the subdirectory's contents (recursively).

gitrepo.working_dir = subdirectory
print "DEBUG: archive working dir is %s" % gitrepo.working_dir
## outputs the value of "subdirectory" but this is not used in a subsequent gitrepo.archive call

I have difficulties finding the "archive" method in the current master. Code search on github only produced a hit on GitPython / lib / git / repo.py (tree: 90d73cd): https://github.com/search?q=repo%3Agitpython-developers%2FGitPython+archive&repo=&langOverride=&start_value=1&type=Everything&language=

(I'm feeling like I'm missing the obvious...)

@maurociancio
Copy link
Author

Yes, I've dug into the code but I couldn't find the appropiate place to add the neeeded argument. Maybe someone else with more understanding of the code base could throw us some pointers.

What I've found is the following, but I know itsn't enough :P:

git grep -n "archive("

git/db/cmd/base.py:847: def archive(self, ostream, treeish=None, prefix=None, *_kwargs):
git/db/cmd/base.py:861: self.git.archive(treeish, *_kwargs)
git/db/interface.py:824: def archive(self, ostream, treeish=None, prefix=None):

Any ideas?

@Byron
Copy link
Member

Byron commented Jul 11, 2012

Sorry for the late reply.

Looking in master is not actually recommended, as the code is bleeding edge and is not even alpha.

In 0.3, what you could try instead is to just use the git command directly, maybe the following snipped helps:

# resolves to git-archive --option-arg --value-arg "value"
repo.git.archive(option_arg=True, value_arg="value")

You can rather easily create fancy command invocations using the git command wrapper - this way you can do everything that you can do with the commandline.

In 0.3, the cmd.py module would be the place to look for more information.

If you ask me, repo.archive isn't really required as the same call can be made much easier using the git command itself - it was in the interface since 0.17 though and could be considered legacy.

Good luck !

@nuarhu
Copy link

nuarhu commented Jul 12, 2012

Hi Byron,

thank you for your reply which is not late at all!

I've (sheepishly) tried your code without modification and this gives me:

git.exc.GitCommandError: 'git archive --value-arg=/absolute/path/to/git/subdir HEAD'
    returned exit status 129: error: unknown option `value-arg=/absolute/path/to/git/subdir'
usage: git archive [options] <tree-ish> [<path>...]

(changed path value and added line break for readability)

UPDATE:

To hand over a value without option like in

usage: git archive [options] <tree-ish> [<path>...]

something like:

gitrepo.archive(open(desttar,'wb'), [('', rev), ('', working_dir)])

seems to be the way to go? However, it returns an error which includes a git call which - when pasted into the commandline - dumps the tar stream to stdout = does what I want. So, either the command which is output by GitCommandError is not the one actually called or something else goes wrong on the way.

    File "./gitrex.py", line 153, in git_archive
      gitrepo.archive(open(desttar,'wb'), [('', rev), ('', working_dir)])
    File "/Library/Python/2.7/site-packages/GitPython-0.3.2.RC1-py2.7.egg/git/repo/base.py", line 759, in archive
      self.git.archive(treeish, **kwargs)
    File "/Library/Python/2.7/site-packages/GitPython-0.3.2.RC1-py2.7.egg/git/cmd.py", line 227, in <lambda>
      return lambda *args, **kwargs: self._call_process(name, *args, **kwargs)
    File "/Library/Python/2.7/site-packages/GitPython-0.3.2.RC1-py2.7.egg/git/cmd.py", line 456, in _call_process
      return self.execute(call, **_kwargs)
    File "/Library/Python/2.7/site-packages/GitPython-0.3.2.RC1-py2.7.egg/git/cmd.py", line 377, in execute
      raise GitCommandError(command, status, stderr_value)
git.exc.GitCommandError: 'git archive  HEAD  /absolute/working/dir' returned exit status 128: fatal: Not a valid object name

(path changed)

Command line:

git archive  HEAD  /absolute/working/dir

dumps the tar output.

@nuarhu
Copy link

nuarhu commented Jul 12, 2012

This actually works (work around): changing the working directory and changing back afterwards.

gitrepo.working_dir = subdir
print "DEBUG: archive working dir is %s" % gitrepo.working_dir
os.chdir(subdir)
os.makedirs(destdir)
desttar = os.path.join(destdir, rev+".tar")
gitrepo.archive(open(desttar,'wb'), treeish=rev, with_keep_cwd=True)
tar = tarfile.open(desttar);
print "DEBUG: archive stored in dir %s" % destdir
tar.extractall(destdir)
os.chdir(self._cwd)

Note that the first line has no effect!

The work around consists of the call "os.chdir" in combination with the "with_keep_cwd=True" argument to the gitrepo.archive command call.

@maurociancio
Copy link
Author

Hello all,
I recently found out that executing "git archive master:folder" creates an archive that only contains the 'folder' directory.
I tested the command using GitPython and it works great.

So, there is no need to hack the source code or to change the current directory as @nuarhu suggested.
This works out of the box ;)

Well done git, well done :)

@vishakhamudgal
Copy link

does archiving supported by github?

@Byron Byron self-assigned this Jan 14, 2015
@Byron Byron closed this as completed in aa1b156 Jan 14, 2015
@Byron
Copy link
Member

Byron commented Jan 14, 2015

You can now watch the live-coding session on youtube.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants