Bug 15917

Summary: fetch2/git: `_contains_lfs` check is falsely triggered by commented lines in .gitattributes
Product: [Build System, Metadata & Runtime] BitBake Reporter: Markus Isaksson <markus.isaksson>
Component: bitbakeAssignee: Markus Isaksson <markus.isaksson>
Status: RESOLVED FIXED QA Contact:
Severity: enhancement    
Priority: Medium CC: poky.bs.watcher, poky.watcher, randy.macleod, richard.purdie
Version: unspecified   
Target Milestone: 5.3   
Hardware: x86   
OS: x86_64   
Whiteboard:
OS type for building Yocto: --- Type of Regression: ---
Verified: Documentation change: No (bug/feature does not impact docs)

Description Markus Isaksson 2025-06-27 08:52:19 UTC
**Environment:**
```
$ bitbake --version
BitBake Build Tool Core version 2.8.0

$ lscpu
Architecture:             x86_64
  CPU op-mode(s):         32-bit, 64-bit
  Address sizes:          43 bits physical, 48 bits virtual
  Byte Order:             Little Endian
CPU(s):                   8
  On-line CPU(s) list:    0-7
Vendor ID:                GenuineIntel
  Model name:             Intel(R) Xeon(R) CPU E5-2695 v4 @ 2.10GHz

$ cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
```

Inspecting master branch in (https://github.com/openembedded/bitbake) shows the same issue

**Description:**
When creating a recipe for a Git repository that contains a `.gitattributes` file with commented-out LFS rules, the `do_fetch` task incorrectly detects LFS content. This causes the fetcher to attempt `git lfs` commands, leading to a build failure on any host where the `git-lfs` client is not installed.

This behavior is confusing because the repository does not actually use or require Git LFS.

**Steps to Reproduce:**

1.  Use a build host that does not have the `git-lfs` package installed.
2.  Create a recipe for a repository containing a `.gitattributes` file with commented-out LFS rules. A specific example is `MicrochipTech/cryptoauthlib` at version 3.7.4 or later. The relevant `.gitattributes` file contains:
    ```
    # Reinstate LFS if pdf sizes increase considerably in the future releases
    # *.pdf filter=lfs diff=lfs merge=lfs -text
    ```
3.  Use the following minimal recipe:
    ```bb
    SRC_URI = "git://github.com/MicrochipTech/cryptoauthlib.git;protocol=https;branch=main"
    SRCREV = "a7bbe3a2d6851dea2ae624b598a823c0fb70d48c" # Corresponds to v3.7.8

    S = "${WORKDIR}/git"
    ```
4.  Attempt to build the recipe (`bitbake cryptoauthlib`).

**Expected Result:**
The `do_fetch` task should successfully clone the repository. Since no files are actually managed by LFS, the fetcher should not attempt any `git lfs` operations.

**Actual Result:**
The `do_fetch` task fails with an error indicating that `git lfs` is not a valid command.

```
WARNING: cryptoauthlib-3.7.8+git-r0 do_fetch: Failed to fetch URL git://github.com/MicrochipTech/cryptoauthlib.git;protocol=https;branch=main, attempting MIRRORS if available
ERROR: cryptoauthlib-3.7.8+git-r0 do_fetch: Fetcher failure: Fetch command export PSEUDO_DISABLED=1; <...more exports...> git -c gc.autoDetach=false -c core.pager=cat -c safe.bareRepository=all lfs ls-files -l a7bbe3a2d6851dea2ae624b598a823c0fb70d48c failed with exit code 1, output:
git: 'lfs' is not a git command. See 'git --help'.

The most similar command is
    log
```

**Analysis:**
The root cause is the implementation of the `_contains_lfs` helper function in `openembedded/bitbake/lib/bb/fetch2/git.py`.

The function uses the following command to detect LFS content:

```python
cmd = "%s grep lfs %s:.gitattributes | wc -l" % (
    ud.basecmd, ud.revision)
```

This command uses `git grep` to find the literal string "lfs" within the `.gitattributes` file. However, `grep` does not account for comment characters (`#`) in this context. It finds the string "lfs" on the commented-out line, causing `wc -l` to return a non-zero value.

This false positive triggers the subsequent LFS logic, leading to the failure.
Comment 1 Randy MacLeod 2025-07-03 14:37:55 UTC
Markus, 

Are you willing and able to submit a patch.
https://docs.yoctoproject.org/contributor-guide/index.html

If so please accept this defect. 
If not, it will be unassigned until someone has time to get to it.
- YP Bug review