Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.appdevforall.k2go.network.data.FileResolvConfWriter;
import org.appdevforall.k2go.network.data.PrefsDnsConfigRepository;
import org.appdevforall.k2go.network.domain.ApplyDnsUseCase;
import org.appdevforall.k2go.proot.data.GuestGitConfigWriter;
import org.appdevforall.k2go.proot.data.PrefsSeccompModeRepository;
import org.appdevforall.k2go.proot.data.ProotEnvironment;
import org.appdevforall.k2go.proot.domain.SeccompFailure;
Expand Down Expand Up @@ -78,6 +79,9 @@ public void executeInContainer(Context context, String rootfsDir, String command
} catch (Exception dnsEx) {
Log.w(TAG, "DNS apply skipped: " + dnsEx.getMessage());
}
// K2GO-558: also before every launch, so a rootfs baked without these git settings
// gets them before the box runs its first git fetch.
GuestGitConfigWriter.ensure(new File(rootfsDir));

// =========================================================
// THE W^X TROJAN HORSE: FAKE TERMUX PREFIX
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* ============================================================================
* Name : GuestGitConfigWriter.java
* Author : AppDevForAll
* Copyright : Copyright (c) 2026 AppDevForAll
* Description : Brings the rootfs /etc/gitconfig in line with GuestGitConfig (K2GO-558).
* ============================================================================
*/
package org.appdevforall.k2go.proot.data;

import android.util.Log;

import org.appdevforall.k2go.proot.domain.GuestGitConfig;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;

public final class GuestGitConfigWriter {

private static final String TAG = "IIAB-GitConfig";

private GuestGitConfigWriter() {}

/**
* No-op when the settings are already in effect. A temp file + rename, so a kill mid-write
* never leaves a broken gitconfig (git refuses to run with one). Synchronized because proot
* launches can overlap and share the temp file. Never throws.
*/
public static synchronized void ensure(File rootfsDir) {
try {
File etc = new File(rootfsDir, "etc");
if (!etc.isDirectory()) {
return;
}
File conf = new File(etc, "gitconfig");
byte[] existing = conf.isFile() ? read(conf) : new byte[0];
String add = GuestGitConfig.toAppend(new String(existing, StandardCharsets.UTF_8));
if (add.isEmpty()) {
return;
}
File tmp = new File(etc, "gitconfig.k2go-tmp");
try (FileOutputStream fos = new FileOutputStream(tmp)) {
fos.write(existing); // the original bytes, unchanged
fos.write(add.getBytes(StandardCharsets.UTF_8));
fos.getFD().sync();
}
if (!tmp.renameTo(conf)) {
tmp.delete();
Log.w(TAG, "could not replace " + conf);
return;
}
Log.i(TAG, "git settings for proot added to " + conf);
} catch (Exception e) {
Log.w(TAG, "gitconfig update skipped", e);
}
}

private static byte[] read(File f) throws java.io.IOException {
try (FileInputStream in = new FileInputStream(f)) {
ByteArrayOutputStream buf = new ByteArrayOutputStream();
byte[] b = new byte[4096];
for (int n; (n = in.read(b)) > 0; ) {
buf.write(b, 0, n);
}
return buf.toByteArray();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* ============================================================================
* Name : GuestGitConfig.java
* Author : AppDevForAll
* Copyright : Copyright (c) 2026 AppDevForAll
* Description : The git settings that keep git objects whole under proot --link2symlink.
* Pure JVM.
* ============================================================================
*/
package org.appdevforall.k2go.proot.domain;

import java.util.Locale;

/**
* K2GO-558: git finalizes new objects with {@code link()}; proot {@code --link2symlink} turns that into
* a symlink to a path that is then removed, so objects go missing and the clone breaks. With
* {@code rename()} (and packs kept whole) nothing goes through link2symlink.
*
* <p>The rootfs bake writes the same settings into {@code /etc/gitconfig}
* ({@code tools/rootfs-builder/build-iiab-rootfs.sh}); this rule brings older rootfs trees in line.
*/
public final class GuestGitConfig {

/** section.key (lower case, as git compares them) -> required value. */
private static final String[][] REQUIRED = {
{"core", "createobject", "createObject", "rename"},
{"transfer", "unpacklimit", "unpackLimit", "1"},
};

private GuestGitConfig() {}

/**
* The text to append to a system gitconfig so that the required settings take effect, or ""
* when they already do. Git uses the last value of a key, so an appended block wins.
*/
public static String toAppend(String existing) {
StringBuilder out = new StringBuilder();
for (String[] r : REQUIRED) {
if (!r[3].equals(lastValue(existing, r[0], r[1]))) {
out.append('[').append(r[0]).append("]\n\t").append(r[2]).append(" = ").append(r[3]).append('\n');
}
}
if (out.length() > 0 && existing != null && !existing.isEmpty() && !existing.endsWith("\n")) {
out.insert(0, '\n');
}
return out.toString();
}

/** The last value of {@code section.key} in a gitconfig text, or null. Subsections are skipped. */
static String lastValue(String text, String section, String key) {
if (text == null) {
return null;
}
String current = null;
String value = null;
for (String raw : text.split("\n", -1)) {
String line = raw.trim();
if (line.isEmpty() || line.startsWith("#") || line.startsWith(";")) {
continue;
}
if (line.startsWith("[")) {
int end = line.indexOf(']');
current = end > 0 ? line.substring(1, end).trim().toLowerCase(Locale.ROOT) : null;
continue;
}
if (!section.equals(current)) {
continue;
}
int eq = line.indexOf('=');
String k = (eq >= 0 ? line.substring(0, eq) : line).trim().toLowerCase(Locale.ROOT);
if (k.equals(key)) {
value = eq >= 0 ? stripComment(line.substring(eq + 1)).trim() : "true";
}
}
return value;
}

private static String stripComment(String v) {
int c = v.indexOf('#');
int s = v.indexOf(';');
int cut = c < 0 ? s : (s < 0 ? c : Math.min(c, s));
return cut < 0 ? v : v.substring(0, cut);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.appdevforall.k2go.proot.domain;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import org.junit.Test;

public class GuestGitConfigTest {

private static final String BOTH =
"[core]\n\tcreateObject = rename\n[transfer]\n\tunpackLimit = 1\n";

@Test
public void emptyOrMissingGetsBothSettings() {
assertEquals(BOTH, GuestGitConfig.toAppend(""));
assertEquals(BOTH, GuestGitConfig.toAppend(null));
}

@Test
public void bakedConfigNeedsNothing() {
String baked = "[pack]\n\twriteReverseIndex = false\n" + BOTH;
assertEquals("", GuestGitConfig.toAppend(baked));
}

@Test
public void oldBakeGetsOnlyTheMissingSettings() {
String old = "[pack]\n\twriteReverseIndex = false\n";
assertEquals(BOTH, GuestGitConfig.toAppend(old));
String half = old + "[core]\n\tcreateObject = rename\n";
assertEquals("[transfer]\n\tunpackLimit = 1\n", GuestGitConfig.toAppend(half));
}

@Test
public void lastValueWinsLikeGit() {
// An earlier rename overridden by a later link is NOT in effect: append again.
String overridden = BOTH + "[core]\n\tcreateobject = link\n";
assertEquals("[core]\n\tcreateObject = rename\n", GuestGitConfig.toAppend(overridden));
}

@Test
public void keysAndSectionsAreCaseInsensitiveAndCommentsIgnored() {
String text = "[CORE]\n CreateObject=rename # proot\n[Transfer]\nunpacklimit = 1 ; keep packs\n";
assertEquals("", GuestGitConfig.toAppend(text));
}

@Test
public void subsectionsAndOtherSectionsDoNotCount() {
String text = "[core \"x\"]\n\tcreateObject = rename\n[user]\n\tunpackLimit = 1\n";
assertEquals(BOTH, GuestGitConfig.toAppend(text));
assertNull(GuestGitConfig.lastValue(text, "core", "createobject"));
}

@Test
public void missingFinalNewlineIsAddedBeforeTheBlock() {
assertEquals("\n" + BOTH, GuestGitConfig.toAppend("[pack]\n\twriteReverseIndex = false"));
}
}
5 changes: 5 additions & 0 deletions tools/rootfs-builder/build-iiab-rootfs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,11 @@ mkdir -p "$ROOTFS/tmp" "$ROOTFS/root"; chmod 1777 "$ROOTFS/tmp"
# Disable them system-wide inside the rootfs so no *.rev is ever created during the
# install, so 'rm -rf /tmp/iiab-wiki' has nothing proot chokes on. Harmless elsewhere.
printf '[pack]\n\twriteReverseIndex = false\n' >> "$ROOTFS/etc/gitconfig"
# K2GO-558: git finalizes objects with link(), which --link2symlink turns into a symlink to a path
# that is then removed, so objects go missing ("bad object HEAD"). rename() is not intercepted,
# and unpackLimit=1 keeps fetched packs whole. The app applies the same settings to older trees
# (proot/domain/GuestGitConfig.java).
printf '[core]\n\tcreateObject = rename\n[transfer]\n\tunpackLimit = 1\n' >> "$ROOTFS/etc/gitconfig"

# --- INTERIM proot-safe environment (should move to iiab-android / upstream) --
# Three independent chroot/proot detections misfire under proot on a desktop/
Expand Down
Loading