Repository: VirtusLab/git-machete
Pull request #1112 (2023-11-27 18:39:47)

If `.git/machete` does not exist, make `git machete add <branch>` add both current branch and the newly-added `<branch>`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/VirtusLab/git-machete/pull/1112?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VirtusLab) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`640eae3`)](https://app.codecov.io/gh/VirtusLab/git-machete/commit/640eae358d5e5ee704cf05f2c144ebfab15fa0d1?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VirtusLab) 98.70% compared to head [(`13db5a2`)](https://app.codecov.io/gh/VirtusLab/git-machete/pull/1112?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VirtusLab) 98.70%.


<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff            @@
##           develop    #1112   +/-   ##
========================================
  Coverage    98.70%   98.70%           
========================================
  Files           12       12           
  Lines         3327     3327           
  Branches       752      752           
========================================
  Hits          3284     3284           
  Misses          27       27           
  Partials        16       16           
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/VirtusLab/git-machete/pull/1112?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VirtusLab).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VirtusLab).


Patch:
diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md
index cc2cde4..37380ad 100644
--- a/RELEASE_NOTES.md
+++ b/RELEASE_NOTES.md
@@ -2,6 +2,8 @@
 
 ## New in git-machete 3.22.0
 
+- fixed: if `.git/machete` doesn't exist, `git machete add <branch>` adds both current branch and the newly-added `<branch>` (not just the latter)
+
 ## New in git-machete 3.21.0
 
 - added: `--removed-from-remote` flag to `git machete slide-out` (contributed by @raylu)
diff --git a/docs/man/git-machete.1 b/docs/man/git-machete.1
index 6692446..6d62ccd 100644
--- a/docs/man/git-machete.1
+++ b/docs/man/git-machete.1
@@ -27,7 +27,7 @@ level margin: \\n[rst2man-indent\\n[rst2man-indent-level]]
 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]]
 .in \\n[rst2man-indent\\n[rst2man-indent-level]]u
 ..
-.TH "GIT-MACHETE" "1" "Dec 01, 2023" "" "git-machete"
+.TH "GIT-MACHETE" "1" "Dec 03, 2023" "" "git-machete"
 .SH NAME
 git-machete \- git-machete 3.22.0
 .sp
diff --git a/git_machete/client.py b/git_machete/client.py
index aa8a21d..f6b7182 100644
--- a/git_machete/client.py
+++ b/git_machete/client.py
@@ -269,10 +269,21 @@ class MacheteClient:
                 if self.ask_if(msg, opt_yes_msg, opt_yes=opt_yes, verbose=verbose) in ('y', 'yes'):
                     # If `--onto` hasn't been explicitly specified, let's try to
                     # assess if the current branch would be a good `onto`.
-                    if self.__roots and not opt_onto:
+                    if not opt_onto:
                         current_branch = self.__git.get_current_branch_or_none()
-                        if current_branch and current_branch in self.managed_branches:
-                            opt_onto = current_branch
+                        if self.__roots:
+                            if current_branch and current_branch in self.managed_branches:
+                                opt_onto = current_branch
+                        else:
+                            if current_branch:
+                                # In this case (empty .git/machete, creating a new branch with `git machete add`)
+                                # it's usually pretty obvious that the current branch needs to be added as root first.
+                                # Let's skip interactive questions so as not to confuse new users.
+                                self.__roots = [current_branch]
+                                self.__managed_branches = [current_branch]
+                                if verbose:
+                                    print(fmt(f"Added branch {bold(current_branch)} as a new root"))
+                                opt_onto = current_branch
                     self.__git.create_branch(branch, out_of, switch_head=switch_head_if_new_branch)
                 else:
                     return


Test patch:
diff --git a/tests/test_add.py b/tests/test_add.py
index 3eb5950..b97b98f 100644
--- a/tests/test_add.py
+++ b/tests/test_add.py
@@ -2,7 +2,8 @@ from pytest_mock import MockerFixture
 
 from .base_test import BaseTest
 from .mockers import (assert_failure, assert_success, mock_input_returning,
-                      mock_input_returning_y, rewrite_branch_layout_file)
+                      mock_input_returning_y, read_branch_layout_file,
+                      rewrite_branch_layout_file)
 
 
 class TestAdd(BaseTest):
@@ -87,7 +88,8 @@ class TestAdd(BaseTest):
         assert_success(
             ['add', '-y', 'foo'],
             'A local branch foo does not exist. Creating out of the current HEAD\n'
-            'Added branch foo as a new root\n'
+            'Added branch master as a new root\n'
+            'Added branch foo onto master\n'
         )
 
         self.patch_symbol(mocker, "builtins.input", mock_input_returning("n"))
@@ -169,6 +171,30 @@ class TestAdd(BaseTest):
             "Use git machete add foo or git machete edit."
         )
 
+    def test_add_new_branch_onto_master_for_fresh_start_with_yes(self) -> None:
+        self.repo_sandbox.new_branch("master").commit("master commit.")
+        assert_success(
+            ['add', '--yes', 'foo'],
+            """
+            A local branch foo does not exist. Creating out of the current HEAD
+            Added branch master as a new root
+            Added branch foo onto master
+            """)
+        assert read_branch_layout_file() == "master\n  foo\n"
+
+    def test_add_new_branch_onto_master_for_fresh_start_without_yes(self, mocker: MockerFixture) -> None:
+        self.repo_sandbox.new_branch("master").commit("master commit.")
+
+        self.patch_symbol(mocker, "builtins.input", mock_input_returning_y)
+        assert_success(
+            ['add', 'foo'],
+            """
+            A local branch foo does not exist. Create out of the current HEAD? (y, N)
+            Added branch master as a new root
+            Added branch foo onto master
+            """)
+        assert read_branch_layout_file() == "master\n  foo\n"
+
     def test_add_as_root_with_onto(self) -> None:
         assert_failure(
             ['add', '--onto', 'foo', '--as-root'],