diff options
-rw-r--r-- | CHANGES | 1 | ||||
-rw-r--r-- | lib/git/repo.py | 31 | ||||
-rw-r--r-- | test/git/test_repo.py | 25 |
3 files changed, 57 insertions, 0 deletions
@@ -68,6 +68,7 @@ Repo - archive_tar_gz and archive_tar and replaced by archive method with different signature * 'commits' method has no max-count of returned commits anymore, it now behaves like git-rev-list +* 'untracked_files' property added, returning all currently untracked files Diff ---- diff --git a/lib/git/repo.py b/lib/git/repo.py index 554c10cb..6edb7f62 100644 --- a/lib/git/repo.py +++ b/lib/git/repo.py @@ -285,6 +285,37 @@ class Repo(object): return False return len(self.git.diff('HEAD', '--').strip()) > 0 + + @property + def untracked_files(self): + """ + Returns + list(str,...) + + Files currently untracked as they have not been staged yet. Paths + are relative to the current working directory of the git command. + + Note + ignored files will not appear here, i.e. files mentioned in .gitignore + """ + # make sure we get all files, no only untracked directores + proc = self.git.commit(untracked_files=True, as_process=True) + stream = iter(proc.stdout) + untracked_files = list() + for line in stream: + if not line.startswith("# Untracked files:"): + continue + # skip two lines + stream.next() + stream.next() + + for untracked_info in stream: + if not untracked_info.startswith("#\t"): + break + untracked_files.append(untracked_info.replace("#\t", "").rstrip()) + # END for each utracked info line + # END for each line + return untracked_files @property def active_branch(self): diff --git a/test/git/test_repo.py b/test/git/test_repo.py index 96d08b91..250974a5 100644 --- a/test/git/test_repo.py +++ b/test/git/test_repo.py @@ -225,3 +225,28 @@ class TestRepo(object): assert_true( tlist ) assert_true( isinstance( tlist[0], basestring ) ) assert_true( len( tlist ) < sum( len(t) for t in tlist ) ) # test for single-char bug + + def test_untracked_files(self): + base = self.repo.git.git_dir + files = (base+"/__test_myfile", base+"/__test_other_file") + num_recently_untracked = 0 + try: + for fpath in files: + fd = open(fpath,"wb") + fd.close() + # END for each filename + untracked_files = self.repo.untracked_files + num_recently_untracked = len(untracked_files) + + # assure we have all names - they are relative to the git-dir + num_test_untracked = 0 + for utfile in untracked_files: + num_test_untracked += os.path.join(base, utfile) in files + assert len(files) == num_test_untracked + finally: + for fpath in files: + if os.path.isfile(fpath): + os.remove(fpath) + # END handle files + + assert len(self.repo.untracked_files) == (num_recently_untracked - len(files)) |