diff options
author | Mike Frysinger <vapier@google.com> | 2020-02-09 03:01:56 -0500 |
---|---|---|
committer | David Pursehouse <dpursehouse@collab.net> | 2020-02-09 23:26:05 +0000 |
commit | f914edca5354141facc7e6c9f7facd77835ed766 (patch) | |
tree | 4d17a0860a4e3576d0d1dd8c95aff256cf1950cf | |
parent | e7c91889a6ff23931d3fbc25481b276e50ed8229 (diff) | |
download | git-repo-f914edca5354141facc7e6c9f7facd77835ed766.tar.gz |
project: unify HEAD path management
Add a helper function to unify the duplication of finding the full
path to the symbolic HEAD ref. This makes it easy to handle git
worktrees where .git is a file rather than a dir/symlink.
Bug: https://crbug.com/gerrit/11486
Change-Id: I9f794f1295ad0d98c7c13622f01ded51e4ba7846
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/254074
Tested-by: Mike Frysinger <vapier@google.com>
Reviewed-by: David Pursehouse <dpursehouse@collab.net>
-rw-r--r-- | project.py | 23 |
1 files changed, 16 insertions, 7 deletions
@@ -1817,6 +1817,18 @@ class Project(object): | |||
1817 | 1817 | ||
1818 | # Branch Management ## | 1818 | # Branch Management ## |
1819 | 1819 | ||
1820 | def GetHeadPath(self): | ||
1821 | """Return the full path to the HEAD ref.""" | ||
1822 | dotgit = os.path.join(self.worktree, '.git') | ||
1823 | if os.path.isfile(dotgit): | ||
1824 | # Git worktrees use a "gitdir:" syntax to point to the scratch space. | ||
1825 | with open(dotgit) as fp: | ||
1826 | setting = fp.read() | ||
1827 | assert setting.startswith('gitdir:') | ||
1828 | gitdir = setting.split(':', 1)[1].strip() | ||
1829 | dotgit = os.path.join(self.worktree, gitdir) | ||
1830 | return os.path.join(dotgit, HEAD) | ||
1831 | |||
1820 | def StartBranch(self, name, branch_merge='', revision=None): | 1832 | def StartBranch(self, name, branch_merge='', revision=None): |
1821 | """Create a new branch off the manifest's revision. | 1833 | """Create a new branch off the manifest's revision. |
1822 | """ | 1834 | """ |
@@ -1856,8 +1868,7 @@ class Project(object): | |||
1856 | except OSError: | 1868 | except OSError: |
1857 | pass | 1869 | pass |
1858 | _lwrite(ref, '%s\n' % revid) | 1870 | _lwrite(ref, '%s\n' % revid) |
1859 | _lwrite(os.path.join(self.worktree, '.git', HEAD), | 1871 | _lwrite(self.GetHeadPath(), 'ref: %s%s\n' % (R_HEADS, name)) |
1860 | 'ref: %s%s\n' % (R_HEADS, name)) | ||
1861 | branch.Save() | 1872 | branch.Save() |
1862 | return True | 1873 | return True |
1863 | 1874 | ||
@@ -1904,8 +1915,7 @@ class Project(object): | |||
1904 | # Same revision; just update HEAD to point to the new | 1915 | # Same revision; just update HEAD to point to the new |
1905 | # target branch, but otherwise take no other action. | 1916 | # target branch, but otherwise take no other action. |
1906 | # | 1917 | # |
1907 | _lwrite(os.path.join(self.worktree, '.git', HEAD), | 1918 | _lwrite(self.GetHeadPath(), 'ref: %s%s\n' % (R_HEADS, name)) |
1908 | 'ref: %s%s\n' % (R_HEADS, name)) | ||
1909 | return True | 1919 | return True |
1910 | 1920 | ||
1911 | return GitCommand(self, | 1921 | return GitCommand(self, |
@@ -1938,8 +1948,7 @@ class Project(object): | |||
1938 | 1948 | ||
1939 | revid = self.GetRevisionId(all_refs) | 1949 | revid = self.GetRevisionId(all_refs) |
1940 | if head == revid: | 1950 | if head == revid: |
1941 | _lwrite(os.path.join(self.worktree, '.git', HEAD), | 1951 | _lwrite(self.GetHeadPath(), '%s\n' % revid) |
1942 | '%s\n' % revid) | ||
1943 | else: | 1952 | else: |
1944 | self._Checkout(revid, quiet=True) | 1953 | self._Checkout(revid, quiet=True) |
1945 | 1954 | ||
@@ -3002,7 +3011,7 @@ class Project(object): | |||
3002 | if self._bare: | 3011 | if self._bare: |
3003 | path = os.path.join(self._project.gitdir, HEAD) | 3012 | path = os.path.join(self._project.gitdir, HEAD) |
3004 | else: | 3013 | else: |
3005 | path = os.path.join(self._project.worktree, '.git', HEAD) | 3014 | path = self._project.GetHeadPath() |
3006 | try: | 3015 | try: |
3007 | with open(path) as fd: | 3016 | with open(path) as fd: |
3008 | line = fd.readline() | 3017 | line = fd.readline() |