Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Termux Git, Empty Object File #3385

Closed
pourmand1376 opened this issue May 9, 2023 · 19 comments
Closed

[Bug]: Termux Git, Empty Object File #3385

pourmand1376 opened this issue May 9, 2023 · 19 comments

Comments

@pourmand1376
Copy link

pourmand1376 commented May 9, 2023

Problem description

I have a git client installed in Termux. I sync it daily with my Github repository. However, once in a while I get these two errors:

remote: Counting objects: 100% (61/61), done.  
remote: Compressing objects: 100% (23/23), done.  
remote: Total 42 (delta 32), reused 29 (delta 19), pack-reused 0  
Unpacking objects: 100% (42/42), 5.63 KiB | 57.00 KiB/s, done.  
error: object file .git/objects/3f/9739eb57cd3ae93d2c344a995379373b07c037 is empty  
fatal: loose object 3f9739eb57cd3ae93d2c344a995379373b07c037 (stored in .git/objects/3f/9739eb57cd3ae93d2c344a995379373b07c037) is corrupt

I can solve them by re-cloning the repo. But I want to know why this happens?

Steps to reproduce the behavior.

Follow this tutorial on here. I think after a week of usage, you would see this error at least once.

I think this is somehow related to termux virtual environment since I get no errors in Windows or Linux.

What is the expected behavior?

There should be no error regarding empty file. It should just work.

What I've tried

I have tried these solutions (none of them worked):

System information

  • Termux application version: 0.118.0
  • Android OS version: 12 SPA1 (MIUI 13.0.9)
  • Device model: Xiaomi Redmi Note 11s
@pourmand1376 pourmand1376 changed the title [Bug]: Termux Git, [Bug]: Termux Git, Empty Object File May 9, 2023
@sylirre
Copy link
Member

sylirre commented May 9, 2023

In what directory your repository is located? If that's a some path under /storage/emulated/0, then assume this is expected behavior. Work with repositories on the internal storage such as Termux home directory instead: /data/data/com.termux/files/home.

The file system bug is easily reproduced using coreutils.

Screenshot_20230509-145208

Nothing to do with Termux.


Note that if you need to make changes in your repo and want to use a GUI editor instead of vim, nano, etc, then you can check whether your code editor supports storage volumes. For example QuickEdit app and few others can edit files inside Termux home directory without root and other non-standard manipulations.

@sylirre sylirre closed this as completed May 9, 2023
@sylirre sylirre removed the bug report label May 9, 2023
@sylirre
Copy link
Member

sylirre commented May 9, 2023 via email

@agnostic-apollo
Copy link
Member

@pourmand1376
Copy link
Author

pourmand1376 commented May 9, 2023

Thanks for the reply.

Actually, my path is in the downloads folder:

/data/data/com.termux/files/home/storage/downloads/obsidian-notes

And that's the issue.

Is there anyway to host my git files in home directory but access them through other apps like obsidian (I think it doesn't support storage volumes)?

@agnostic-apollo
Copy link
Member

Termux supports SAF document provider, check second link above. If your app supports it, then you can use it.

@sylirre
Copy link
Member

sylirre commented May 9, 2023

Obsidian seems to support SAF. But for some reason I can't select the wanted storage volume. Access to the root directory of shared storage is blocked either and it is possible to select only sub directory (scoped storage takes action). Using Android 13, all permissions are granted to Obsidian app.

There is a way to split the git repo and checkout directory which can eliminate the file system issue. Example:

git --git-dir /path/to/bare-git-repo --work-tree /sdcard/Download/obsidian-notes <operation>

@agnostic-apollo
Copy link
Member

You need to select termux home, not shared storage. Both are separate SAF providers.

@sylirre
Copy link
Member

sylirre commented May 9, 2023

The issue with Obsidian is that it is not possible to select Termux home or any other provider except directory on shared storage. The picker interface looks restricted in comparison to what is shown when using other applications.

Comparison of pickers opened by Obsidian and QuickEdit apps:
screenshots

@agnostic-apollo
Copy link
Member

agnostic-apollo commented May 9, 2023

Sorry, seem to have missed your earlier comment. OP should contact the dev to add support I guess.

Selecting root of shared storage and Downloads directory is not allowed by android 11.

https://developer.android.com/about/versions/11/privacy/storage#directory-access

@pourmand1376
Copy link
Author

Obsidian seems to support SAF. But for some reason I can't select the wanted storage volume. Access to the root directory of shared storage is blocked either and it is possible to select only sub directory (scoped storage takes action). Using Android 13, all permissions are granted to Obsidian app.

There is a way to split the git repo and checkout directory which can eliminate the file system issue. Example:

git --git-dir /path/to/bare-git-repo --work-tree /sdcard/Download/obsidian-notes <operation>

Your solution seems appropriate. I implemented it in my workflow and currently it is working well. Let's see if it has any problems.

@Swayam2004
Copy link

Swayam2004 commented Sep 22, 2023

Obsidian seems to support SAF. But for some reason I can't select the wanted storage volume. Access to the root directory of shared storage is blocked either and it is possible to select only sub directory (scoped storage takes action). Using Android 13, all permissions are granted to Obsidian app.

There is a way to split the git repo and checkout directory which can eliminate the file system issue. Example:

git --git-dir /path/to/bare-git-repo --work-tree /sdcard/Download/obsidian-notes <operation>

Thanks for this solution/work-around, been struggling at it for around a week, but this finally solved it. Thank you very much

@jwhenry28
Copy link

@pourmand1376 @Swayam2004 Can you post the full command(s) that you ran to fix this? Do I need to add --git-dir and --work-tree to every single git command that Obsidian runs on my Android device?

@sylirre
Copy link
Member

sylirre commented Dec 16, 2023

Git dir and work tree should be specified every time.

The reason behind a fix/workaround is to place got repository on a file system that works in a way expected by git.

No one-time command exist for a permanent fix.

@pourmand1376
Copy link
Author

@pourmand1376 @Swayam2004 Can you post the full command(s) that you ran to fix this? Do I need to add --git-dir and --work-tree to every single git command that Obsidian runs on my Android device?

I've written a simple script to solve this issue. See here.

This way you can sync your obsidian on mobile only by typing 0 (or any command you like) with git.

@jwhenry28
Copy link

@pourmand1376 Thanks - that blog post was super informative. So you have abandoned the obsidian-git plugin entirely on Android in favor of your solution? Or is there a way to combine your solution with obsidian-git on Android?

@pourmand1376
Copy link
Author

@pourmand1376 Thanks - that blog post was super informative. So you have abandoned the obsidian-git plugin entirely on Android in favor of your solution? Or is there a way to combine your solution with obsidian-git on Android?

It basically disabled it on android. I use it for other OSes like windows and Linux.

@shadyabhi
Copy link

@pourmand1376 With the git command which is prescribed as a solution , the --work-tree is still under ~/storage/shared, just that the --git-dir.

However, as per the comment from @sylirre, the bug here seems to be around managing files under ~/storage/shared by coreutils, so kinda like us running git from Termux to manage a file under ~/storage/shared.
@sylirre Can you please clarify what exactly is the bug here? Is the recommended approach of different --work-tree and --git-dir still at risk of having empty files under --git-dir?

@mehanoid
Copy link

Is the recommended approach of different --work-tree and --git-dir still at risk of having empty files under --git-dir?

I haven't encountered this, but I have sometimes encountered empty files inside --work-tree.
Then I added the following pre-commit hook (example snippet for markdown files, only if you don't expect that your repository should really contain empty files). Another dirty hack, but I haven't had such problems since then.

# Get the list of modified files with .md extension and read them into the array
git diff --name-only --cached --diff-filter=M | grep -E "\.md$" | while read -d $'\n' file
do
  # Check if the file is empty
  if [ ! -s "$file" ]
  then
    # Roll back the file to its last commit state
    git checkout HEAD -- "$file"
    echo "Restored $file to its last commit state"
  fi
done

@TomJo2000
Copy link
Member

TomJo2000 commented Nov 18, 2024

Please do not revive old resolved issue threads.
If you have a similar question you can mention the old issue by number (e.g. #3385) in a new discussion or issue.

But if we're here anyway, may as well answer the question.
The issue is that external/shared storage does not have all of the necessary permissions for Termux's Git to properly manage a repository stored there.
To avoid this please move your git repositories (and/or git worktrees) to Termux's internal storage under ~ (e.g. /data/data/com.termux/files/home).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants