ScottDuckworth / python-anyvcs

A Python abstraction layer for multiple version control systems
BSD 3-Clause "New" or "Revised" License
11 stars 4 forks source link

checking for zero commits #3

Closed OEP closed 11 years ago

OEP commented 11 years ago

Different types of empty repositories seem to behave differently. The main offender is git, which throws CalledProcessErrors on a lot of its commands.

For SVN and Hg, there is a concept of a "0" revision which can safely be used as the rev argument. There's not an equivalent for git (well, that I know of). I can think of two things to work around this:

  1. Label operations on zero-commit repos undefined behavior and provide a safe way to check for zero commits
  2. Make git behave like the others in zero-commit case.

I like (1) since it seems easier and respects the different philosophies about zero-commits, plus (2) seems kind of fudgy and counter-intuitive.

Seems like we need a uniform way of checking of the repo has zero commits since git's log() command fails in zero commit case.

EDIT: I forgot to add some info about how to check for zero commits:

  1. Git: "git rev-parse HEAD" outputs the rev only if there are commits (with zero return status if there are none). This seems to fail only in the most pathological of cases (http://stackoverflow.com/questions/5491832/how-can-i-check-whether-a-git-repository-has-any-commits-in-it)
  2. SVN: len(self.log()) == 1
  3. HG: len(self.log()) == 0
ScottDuckworth commented 11 years ago

The only problem I've found is when GitRepo.log() is used on an empty repo. Everything else seems to behave normally. Have you found otherwise? If that's the only place where git differs, I think we should probably handle that case gracefully and return an empty list.

Defining a VCSRepo.empty() does seem like a good idea. I'll go ahead and add it.

"git rev-parse HEAD" seems to behave differently on bare and non-bare repos. On an empty bare repo it prints "HEAD" to stdout and exits 0. On an empty non-bare repo it does that then spews some stuff on stderr and exits 128. While the focus is on bare repos, I'd like to keep it working on non-bare repos if possible too.

ScottDuckworth commented 11 years ago

Implemented in e3da32f49d61750cdbf41d47c3afee1b32e1045a

OEP commented 11 years ago

(leaving closed, just clarifying)

I had trouble remembering what I meant. I think what I meant is that it is impossible to use GitRepo.ls() on an empty repository because there is nothing you can use for the 'rev' argument, whereas you can use '0' for Hg and SVN. This makes sense by nature of how Git works, but it ends up being kind of confusing since you can get away with it on Hg and SVN. It seems like leaving that as undefined behavior and providing VCSRepo.empty() is a safe way to do things.