summaryrefslogtreecommitdiff
path: root/linaropy/git/worktree.py
blob: 7da0cf8ea3d40f5cb3ae0693e34c408a241c1e12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
import unittest
import logging
import os

import uuid

from ..proj import Proj

from ..cd import cd
from gitrepo import GitRepo

from clone import Clone

from sh import git, ErrorReturnCode, rm

import shutil


class Worktree(GitRepo):

    @classmethod
    def create(cls, proj, repo, path, localBranch, trackedBranch=None):
        """ Factory function for creating worktrees in new directories.

        Parameters
        ----------
        proj : Proj
            Temporary project directory
        repo : Clone
            Repo that we're creating a worktree for.
        path
            Path to the proposed worktree (must not exist).
        localBranch
            The name of the local branch that the created worktree should be on.
        trackedBranch
            Branch to base the local branch on (only valid when the local branch
            is a new branch).
        """
        if not isinstance(proj, Proj):
            raise TypeError('Unsupported input proj type')

        # Technically, we could support Worktree here as well, but that's
        # unlikely to be very useful in practice so there's no use complicating
        # our testing and our interfaces with this (repo needs to have a
        # clonedir method)
        if not isinstance(repo, Clone):
            raise TypeError('Unsupported input repo type')

        path = str(path)

        # An empty path would result in an empty variable expansion
        # and a malformed git worktree add expression.
        if path == "" or path == "None":
            raise TypeError(
                'You must specify a worktree directory when creating a Worktree')

        if not os.path.isabs(path):
            raise TypeError('Worktree path must be absolute')

        if os.path.exists(path):
            raise EnvironmentError('Worktree path %s already exists' % path)

        if localBranch is None:
            raise TypeError(
                'You must specify a local branch when creating a Worktree')

        localBranch = str(localBranch)

        if not GitRepo.is_valid_branch_name(localBranch):
            raise TypeError('Invalid local branch %s' % localBranch)

        if trackedBranch is None:
            trackedBranch = "master"
        else:
            if repo.branch_exists(localBranch):
                raise TypeError(
                    'Local branch %s already exists; it is invalid to provide a tracked branch' %
                    localBranch)

            trackedBranch = str(trackedBranch)

            if not GitRepo.is_valid_branch_name(trackedBranch):
                raise TypeError(
                    'Invalid tracked branch %s' % trackedBranch)

        if not repo.branch_exists(trackedBranch):
            raise EnvironmentError(
                'Tracked branch %s does not exist in repo %s' %
                (trackedBranch, repo.clonedir()))

        try:
            # worktree add needs to be called inside the repo directory
            with cd(repo.clonedir()):
                logging.info(
                    "Worktree(): calling git worktree add in %s with branch %s tracking %s " %
                    (path, localBranch, trackedBranch))
                if repo.branch_exists(localBranch):
                    git("worktree", "add", path, localBranch)
                else:
                    git("worktree", "add", "-b", localBranch, path,
                        trackedBranch)
        except ErrorReturnCode as exc:
            raise EnvironmentError("Unable to create a git worktree")

        return cls(proj, path)

    def __init__(self, proj, path):
        """
        Create a worktree object representing the worktree at the given path.
        The worktree must already exist.

        Parameters
        ----------
        proj : Proj
            Temporary project directory.
        path
            Path to the worktree.
        """
        super(Worktree, self).__init__(proj)

        if path is None:
            raise TypeError(
                'Must specify worktree path when creating a worktree')

        if not os.path.isdir(path):
            raise EnvironmentError('%s does not name a directory' % path)

        if not os.path.isfile(os.path.join(path, ".git")):
            # A worktree always contains a .git file in its root (as opposed to
            # a .git directory, as contained by the main working tree obtained
            # with git init or git clone).
            # Note that we are VERY specific about where we want that .git file
            # to live - using git rev-parse could mislead us into thinking that
            # any subdirectory within a worktree is itself a worktree, which
            # makes it impossible to detect complex hierarchies of worktrees.
            raise EnvironmentError('%s is not a worktree' % path)
        self.repodir = path

    def get_original_repo(self):
        # The git common dir should point to the .git directory of the repo that
        # the worktree was created from. We can then strip the .git to get the
        # path to the original repo.
        with cd(self.repodir):
            commonDir = str(git("rev-parse", "--git-common-dir"))[:-1]
            repo, _ = os.path.split(commonDir)
            return repo

    def clean(self, deleteBranch, forceBranchDelete=False):
        """ Clean up the current worktree.

        Delete its directory and run prune on the original repo. If deleteBranch
        is true, also delete the branch that is checked out in the worktree, but
        only if it is merged. To delete an unmerged branch, set forceBranchDelete
        to true.

        It is an error to invoke this method on a worktree whose directory has
        already been deleted (either by another call to clean or through any
        other means).
        """
        if forceBranchDelete and not deleteBranch:
            raise TypeError(
                "Can't force branch deletion if deleteBranch is false")

        if not os.path.isdir(self.repodir):
            raise EnvironmentError('Worktree directory not found: %s' %
                                   self.repodir)

        try:
            branch = self.getbranch()
        except Exception as exc:
            raise EnvironmentError('Failed to get current branch for %s' %
                                   self.repodir)

        originalRepo = self.get_original_repo()
        try:
            shutil.rmtree(self.repodir)
            logging.info("Worktree clean: removed worktree directory %s" %
                         self.repodir)
        except Exception as exc:
            raise EnvironmentError('Failed to remove worktree directory: %s' %
                                   str(exc))

        with cd(originalRepo):
            try:
                git("worktree", "prune")
                logging.info("Worktree clean: pruned repo %s" % originalRepo)
            except ErrorReturnCode:
                raise EnvironmentError(
                    'Worktree directory was removed, but git prune failed')

            if deleteBranch:
                try:
                    self.delete_branch(branch, forceBranchDelete)
                    logging.info("Worktree clean: deleted branch %s" % branch)
                except EnvironmentError as exc:
                    raise EnvironmentError(
                        'Worktree directory %s was removed, but branch deletion failed: %s' %
                        (self.repodir, str(exc)))


class TestWorktree(unittest.TestCase):
    testdirprefix = "WorktreeUT"

    # TODO: these are duplicated in the GitRepo tests - reuse them
    def __create_dummy_commit(self):
        filename = "file" + str(uuid.uuid4())
        open(filename, "a").close()
        git("add", filename)
        git("commit", "-m", "Branches without commits confuse git")

    def __get_current_branch(self):
        branch = str(git("rev-parse", "--abbrev-ref", "HEAD"))
        # git rev-parse returns a trailing newline that we must get rid of
        return branch[:-1]

    def setUp(self):
        self.proj = Proj(prefix=TestWorktree.testdirprefix)

        repoPath = str(os.path.join(self.proj.projdir, "repo"))
        os.makedirs(repoPath)

        with cd(repoPath):
            git("init")
            self.__create_dummy_commit()

        self.repo = Clone(self.proj, repoPath)

    def tearDown(self):
        # We clean up the entire proj directory between tests in order to ensure
        # that no state survives between tests. The proj directory contains not
        # only the clone that we use for each test (where we don't want leftover
        # branches) but also various worktree directories (which may have
        # different names between tests, e.g. worktreedir, worktree1,
        # unimportantworktreedir etc). We could rely on each test to clean up
        # after itself, but it's safer to just nuke everything out of
        # existence.
        self.proj.cleanup()

    def test_worktree(self):
        worktreePath = str(os.path.join(self.proj.projdir, "worktreedir"))
        worktreeBranch = "worktreebranch"

        self.worktree = Worktree.create(self.proj, self.repo, worktreePath,
                                        worktreeBranch, "master")

        self.assertTrue(os.path.isdir(worktreePath),
                        "Failed to create worktree directory")

        with cd(worktreePath):
            self.assertEqual(self.__get_current_branch(),
                             worktreeBranch,
                             "Worktree is on the wrong branch")

    def test_worktree_track_existing(self):
        worktreePath = str(os.path.join(self.proj.projdir, "worktreedir"))
        worktreeBranch = "worktreebranch"
        parentBranch = "parentbranch"

        with cd(self.repo.clonedir()):
            git("checkout", "-b", parentBranch)
            self.__create_dummy_commit()

        self.worktree = Worktree.create(self.proj, self.repo, worktreePath,
                                        worktreeBranch, parentBranch)

        with cd(worktreePath):
            self.assertEqual(git("rev-parse", worktreeBranch),
                             git("rev-parse", parentBranch),
                             "Worktree branch is not based on the parent branch")

    def test_worktree_track_new(self):
        worktreePath = str(os.path.join(self.proj.projdir, "worktreedir"))
        worktreeBranch = "worktreebranch"
        parentBranch = "parentbranch"

        with self.assertRaises(EnvironmentError) as context:
            self.worktree = Worktree.create(self.proj, self.repo, worktreePath,
                                            worktreeBranch, parentBranch)

        self.assertEqual(str(context.exception),
                         "Tracked branch %s does not exist in repo %s" %
                         (parentBranch, self.repo.clonedir()))

    def test_worktree_track_default(self):
        worktreePath = str(os.path.join(self.proj.projdir, "worktreedir"))
        worktreeBranch = "worktreebranch"

        self.worktree = Worktree.create(self.proj, self.repo, worktreePath,
                                        worktreeBranch)

        self.assertTrue(os.path.isdir(worktreePath),
                        "Failed to create worktree directory")

        with cd(worktreePath):
            self.assertEqual(self.__get_current_branch(),
                             worktreeBranch,
                             "Worktree is on the wrong branch")
            self.assertEqual(git("rev-parse", worktreeBranch),
                             git("rev-parse", "master"),
                             "Worktree branch is not based on master")

    def test_worktree_track_invalid(self):
        worktreePath = os.path.join(
            self.proj.projdir, "unimportantworktreedir")
        with self.assertRaises(TypeError) as context:
            self.worktree = Worktree.create(self.proj, self.repo, worktreePath,
                                            "worktreebranch",
                                            "invalid branch name")

        self.assertEqual(str(context.exception),
                         "Invalid tracked branch invalid branch name")

    def test_worktree_local_invalid(self):
        worktreePath = os.path.join(
            self.proj.projdir, "unimportantworktreedir")
        with self.assertRaises(TypeError) as context:
            self.worktree = Worktree.create(self.proj, self.repo, worktreePath,
                                            "invalid branch name")

        self.assertEqual(str(context.exception),
                         "Invalid local branch invalid branch name")

        with self.assertRaises(TypeError) as context:
            self.worktree = Worktree.create(self.proj, self.repo, worktreePath,
                                            None)

        self.assertEqual(str(context.exception),
                         "You must specify a local branch when creating a Worktree")

    def test_worktree_local_existing(self):
        worktreePath = str(os.path.join(self.proj.projdir, "worktreedir"))
        worktreeBranch = "worktreebranch"

        with cd(self.repo.clonedir()):
            git("checkout", "-b", worktreeBranch)
            self.__create_dummy_commit()
            git("checkout", "master")

        self.worktree = Worktree.create(self.proj, self.repo, worktreePath,
                                        worktreeBranch)

        with cd(worktreePath):
            self.assertEqual(self.__get_current_branch(),
                             worktreeBranch,
                             "Worktree is on the wrong branch")

    def test_worktree_local_checked_out(self):
        worktreePath = str(os.path.join(self.proj.projdir, "worktreedir"))
        worktreeBranch = "worktreebranch"

        with cd(self.repo.clonedir()):
            git("checkout", "-b", worktreeBranch)
            self.__create_dummy_commit()

        with self.assertRaises(EnvironmentError) as context:
            self.worktree = Worktree.create(self.proj, self.repo, worktreePath,
                                            worktreeBranch)

        self.assertEqual(str(context.exception),
                         "Unable to create a git worktree")

    def test_worktree_track_with_existing_local(self):
        worktreePath = str(os.path.join(self.proj.projdir, "worktreedir"))
        worktreeBranch = "worktreebranch"

        with cd(self.repo.clonedir()):
            git("checkout", "-b", worktreeBranch)
            self.__create_dummy_commit()
            git("checkout", "master")

        with self.assertRaises(TypeError) as context:
            self.worktree = Worktree.create(self.proj, self.repo, worktreePath,
                                            worktreeBranch, "master")

        self.assertEqual(str(context.exception),
                         "Local branch worktreebranch already exists; it is invalid to provide a tracked branch")

    def test_worktree_dir_existing(self):
        worktreePath = str(os.path.join(self.proj.projdir, "exists"))

        os.makedirs(worktreePath)

        with self.assertRaises(EnvironmentError) as context:
            self.worktree = Worktree.create(self.proj, self.repo, worktreePath,
                                            "existingdir")

        self.assertEqual(str(context.exception),
                         "Worktree path %s already exists" %
                         worktreePath)

    def test_worktree_dir_invalid(self):
        with self.assertRaises(TypeError) as context:
            self.worktree = Worktree.create(self.proj, self.repo, None,
                                            "invalidpath")

        self.assertEqual(str(context.exception),
                         "You must specify a worktree directory when creating a Worktree")

        with self.assertRaises(TypeError) as context:
            self.worktree = Worktree.create(self.proj, self.repo, "",
                                            "invalidpath")

        self.assertEqual(str(context.exception),
                         "You must specify a worktree directory when creating a Worktree")

    def test_worktree_dir_absolute(self):
        worktreePath = os.path.join(self.proj.projdir, "worktreedir")
        worktreePath = str(os.path.abspath(worktreePath))

        self.worktree = Worktree.create(self.proj, self.repo, worktreePath,
                                        "worktreebranch")

        self.assertTrue(os.path.isdir(worktreePath),
                        "Failed to create worktree directory")

    def test_worktree_dir_relative(self):
        startPath = os.path.join(self.proj.projdir, "start", "here")
        worktreePath = os.path.join(self.proj.projdir, "worktreedir")

        os.makedirs(startPath)
        with cd(startPath):
            relativePath = os.path.relpath(worktreePath, startPath)

            with self.assertRaises(TypeError) as context:
                self.worktree = Worktree.create(self.proj, self.repo,
                                                relativePath, "worktreebranch")

            self.assertTrue(str(context.exception),
                            "Worktree path must be absolute")

    def test_worktree_dir_missing_hops(self):
        worktreePath = str(os.path.join(self.proj.projdir, "none", "of",
                                        "these", "exist", "yet"))

        self.worktree = Worktree.create(self.proj, self.repo, worktreePath,
                                        "worktreebranch")

        self.assertTrue(os.path.isdir(worktreePath),
                        "Failed to create worktree directory")

    def test_worktree_clone_invalid(self):
        worktreePath = str(os.path.join(self.proj.projdir, "worktreedir"))

        with self.assertRaises(TypeError) as context:
            self.worktree = Worktree.create(self.proj, "not a clone",
                                            worktreePath, "worktreebranch")

        self.assertEqual(str(context.exception), "Unsupported input repo type")

        with self.assertRaises(TypeError) as context:
            self.worktree = Worktree.create(self.proj, None, worktreePath,
                                            "worktreebranch")

        self.assertEqual(str(context.exception), "Unsupported input repo type")

    def test_worktree_proj_invalid(self):
        worktreePath = str(os.path.join(self.proj.projdir, "worktreedir"))

        with self.assertRaises(TypeError) as context:
            self.worktree = Worktree.create("not a proj", self.repo,
                                            worktreePath, "worktreebranch")

        self.assertEqual(str(context.exception), "Unsupported input proj type")

        with self.assertRaises(TypeError) as context:
            self.worktree = Worktree.create(None, self.repo, worktreePath,
                                            "worktreebranch")

        self.assertEqual(str(context.exception), "Unsupported input proj type")

    def test_worktree_multiple_calls(self):
        worktreePath1 = os.path.join(self.proj.projdir, "worktree1")
        branch1 = "branch1"
        track1 = "track1"

        worktreePath2 = os.path.join(self.proj.projdir, "worktree2")
        branch2 = "branch2"
        track2 = "track2"

        with cd(self.repo.clonedir()):
            git("checkout", "-b", track1)
            self.__create_dummy_commit()

            git("checkout", "-b", track2)
            self.__create_dummy_commit()

        self.worktree1 = Worktree.create(self.proj, self.repo, worktreePath1,
                                         branch1, track1)

        self.worktree2 = Worktree.create(self.proj, self.repo, worktreePath2,
                                         branch2, track2)

        self.assertTrue(os.path.isdir(worktreePath1),
                        "Failed to create worktree directory")
        self.assertTrue(os.path.isdir(worktreePath2),
                        "Failed to create worktree directory")

        with cd(worktreePath1):
            self.assertEqual(self.__get_current_branch(),
                             branch1,
                             "Worktree is on the wrong branch")

            self.assertEqual(git("rev-parse", branch1),
                             git("rev-parse", track1),
                             "Worktree branch is not based on the correct branch")

        with cd(worktreePath2):
            self.assertEqual(self.__get_current_branch(),
                             branch2,
                             "Worktree is on the wrong branch")

            self.assertEqual(git("rev-parse", branch2),
                             git("rev-parse", track2),
                             "Worktree branch is not based on the correct branch")

    def test_worktree_no_path(self):
        with self.assertRaises(TypeError) as context:
            self.worktree = Worktree(self.proj, None)

        self.assertEqual(str(context.exception),
                         "Must specify worktree path when creating a worktree")

    def test_worktree_invalid_path(self):
        worktreePath = os.path.join(self.proj.projdir, "does", "not", "exist")

        with self.assertRaises(EnvironmentError) as context:
            self.worktree = Worktree(self.proj, worktreePath)

        self.assertEqual(str(context.exception),
                         "%s does not name a directory" %
                         worktreePath)

        worktreePath = os.path.join(self.proj.projdir, "file")

        open(worktreePath, "a").close()

        with self.assertRaises(EnvironmentError) as context:
            self.worktree = Worktree(self.proj, worktreePath)

        self.assertEqual(str(context.exception),
                         "%s does not name a directory" % worktreePath)

    def test_not_a_worktree(self):
        worktreePath = os.path.join(self.proj.projdir, "worktreedir")

        # Test that an empty directory is not mistaken for a worktree.
        os.makedirs(worktreePath)

        with self.assertRaises(EnvironmentError) as context:
            self.worktree = Worktree(self.proj, worktreePath)

        self.assertEqual(str(context.exception),
                         "%s is not a worktree" % worktreePath)

        # Test that a non-empty directory that doesn't contain anything
        # git-related is not mistaken for a worktree.
        os.makedirs(os.path.join(worktreePath, "notempty"))

        with self.assertRaises(EnvironmentError) as context:
            self.worktree = Worktree(self.proj, worktreePath)

        self.assertEqual(str(context.exception),
                         "%s is not a worktree" % worktreePath)

        # Test that a main git workting tree (obtained with git init or git
        # clone) isn't mistaken for a worktree.
        with self.assertRaises(EnvironmentError) as context:
            self.worktree = Worktree(self.proj, self.repo.clonedir())

        self.assertEqual(str(context.exception),
                         "%s is not a worktree" % self.repo.clonedir())

        # Test that a directory that contains a worktree as a subdirectory is
        # not mistaken for a worktree.
        childWorktreePath = os.path.join(worktreePath, "aworktree")
        with cd(self.repo.clonedir()):
            git("worktree", "add", "-b", "wokrtreebranch1", childWorktreePath)

        with self.assertRaises(EnvironmentError) as context:
            self.worktree = Worktree(self.proj, worktreePath)

        self.assertEqual(str(context.exception),
                         "%s is not a worktree" % worktreePath)

        # Test that a subdirectory of a worktree is not mistaken for a
        # worktree.
        worktreeSubdirPath = os.path.join(childWorktreePath, "notaworktree")
        os.makedirs(worktreeSubdirPath)

        with self.assertRaises(EnvironmentError) as context:
            self.worktree = Worktree(self.proj, worktreeSubdirPath)

        self.assertEqual(str(context.exception),
                         "%s is not a worktree" % worktreeSubdirPath)

    def test_get_original_repo(self):
        worktreePath = os.path.join(self.proj.projdir, "worktreedir")
        worktreeBranch = "worktreebranch"

        with cd(self.repo.clonedir()):
            git("worktree", "add", "-b", worktreeBranch, worktreePath)

        worktree = Worktree(self.proj, worktreePath)
        self.assertEqual(worktree.get_original_repo(), self.repo.clonedir())

    def test_cleanup(self):
        worktreePath = os.path.join(self.proj.projdir, "worktreedir")
        worktreeBranch = "worktreebranch"

        with cd(self.repo.clonedir()):
            git("worktree", "add", "-b", worktreeBranch, worktreePath)
            self.assertTrue(os.path.isdir(worktreePath))

        self.worktree = Worktree(self.proj, worktreePath)
        self.worktree.clean(False)

        self.assertFalse(os.path.isdir(worktreePath))

        with cd(self.repo.clonedir()):
            git("rev-parse", worktreeBranch)

    def test_cleanup_delete_merged_branch(self):
        worktreePath = os.path.join(self.proj.projdir, "worktreedir")
        worktreeBranch = "worktreebranch"

        with cd(self.repo.clonedir()):
            git("worktree", "add", "-b", worktreeBranch, worktreePath)
            self.assertTrue(os.path.isdir(worktreePath))

            with cd(worktreePath):
                self.__create_dummy_commit()

            git("merge", worktreeBranch)

        self.worktree = Worktree(self.proj, worktreePath)
        self.worktree.clean(True)

        self.assertFalse(os.path.isdir(worktreePath))

        with cd(self.repo.clonedir()):
            with self.assertRaises(ErrorReturnCode) as context:
                git("rev-parse", worktreeBranch)

    def test_cleanup_delete_unmerged_branch(self):
        worktreePath = os.path.join(self.proj.projdir, "worktreedir")
        worktreeBranch = "worktreebranch"

        with cd(self.repo.clonedir()):
            git("worktree", "add", "-b", worktreeBranch, worktreePath)
            self.assertTrue(os.path.isdir(worktreePath))

            with cd(worktreePath):
                self.__create_dummy_commit()

        self.worktree = Worktree(self.proj, worktreePath)

        with self.assertRaises(EnvironmentError) as context:
            self.worktree.clean(True)

        self.assertRegexpMatches(str(context.exception),
                                 "branch deletion failed:")
        self.assertRegexpMatches(str(context.exception),
                                 "not fully merged")

        self.assertFalse(os.path.isdir(worktreePath))

        with cd(self.repo.clonedir()):
            git("rev-parse", worktreeBranch)

    def test_cleanup_force_delete_branch(self):
        worktreePath = os.path.join(self.proj.projdir, "worktreedir")
        worktreeBranch = "worktreebranch"

        with cd(self.repo.clonedir()):
            git("worktree", "add", "-b", worktreeBranch, worktreePath)
            self.assertTrue(os.path.isdir(worktreePath))

            with cd(worktreePath):
                self.__create_dummy_commit()

        self.worktree = Worktree(self.proj, worktreePath)
        self.worktree.clean(True, True)

        self.assertFalse(os.path.isdir(worktreePath))

        with cd(self.repo.clonedir()):
            with self.assertRaises(ErrorReturnCode) as context:
                git("rev-parse", worktreeBranch)

    def test_cleanup_invalid_force(self):
        worktreePath = os.path.join(self.proj.projdir, "worktreedir")
        worktreeBranch = "worktreebranch"

        with cd(self.repo.clonedir()):
            git("worktree", "add", "-b", worktreeBranch, worktreePath)
            self.assertTrue(os.path.isdir(worktreePath))

            with cd(worktreePath):
                self.__create_dummy_commit()

        self.worktree = Worktree(self.proj, worktreePath)

        with self.assertRaises(TypeError) as context:
            self.worktree.clean(False, True)

        self.assertEqual(str(context.exception),
                         "Can't force branch deletion if deleteBranch is false")

        self.assertTrue(os.path.isdir(worktreePath))

        with cd(self.repo.clonedir()):
            git("rev-parse", worktreeBranch)

    def test_cleanup_already_clean(self):
        worktreePath = os.path.join(self.proj.projdir, "worktreedir")
        worktreeBranch = "worktreebranch"

        with cd(self.repo.clonedir()):
            git("worktree", "add", "-b", worktreeBranch, worktreePath)
            self.assertTrue(os.path.isdir(worktreePath))

        self.worktree = Worktree(self.proj, worktreePath)

        rm("-rf", worktreePath)

        with self.assertRaises(EnvironmentError) as context:
            self.worktree.clean(False)

        self.assertRegexpMatches(str(context.exception),
                                 'Worktree directory not found: %s' %
                                 worktreePath)

if __name__ == '__main__':
    # logging.basicConfig(level="INFO")
    unittest.main()