优化gitdiff.py中文名文件处理
author孙春阳 <snickersun@163.com>
Sat, 23 Aug 2025 07:25:48 +0000 (15:25 +0800)
committer孙春阳 <snickersun@163.com>
Sat, 23 Aug 2025 07:25:48 +0000 (15:25 +0800)
cat1modem/src/quecpPython_SLIP接口说明.md [new file with mode: 0644]
gitdiff.py

diff --git a/cat1modem/src/quecpPython_SLIP接口说明.md b/cat1modem/src/quecpPython_SLIP接口说明.md
new file mode 100644 (file)
index 0000000..5a7e909
--- /dev/null
@@ -0,0 +1,179 @@
+###SLIP-串口网卡协议\r
+  \r
+模块功能: 通过串口slip协议,建立网络通讯\r
+#### slip网卡构建\r
+>    **slip.construct(uart, slip_type, external_ip)**\r
+\r
+构建串口网卡\r
+* 参数\r
+\r
+|  参数   | 类型  | 说明 |\r
+|  ----  | ----  | ---|\r
+| uart  | int | 根据串口wiki说明,指定串口号 |\r
+| slip_type  | int | 网卡类型 |\r
+| external_ip  | int | 指令默认网卡的ip,默认填0 |\r
+\r
+* slip_type参数说明\r
+\r
+|  序号   | 类型 | 说明 |\r
+|  ----  | ---- | ---- | \r
+| 0  | SLIP_INNER | 作为内部网卡,此时使用lte连接外部网络 |\r
+| 1  | SLIP_OUTER | 作为外部网卡,此时通过esp连接外部网络|\r
+\r
+* 返回值  \r
+    成功返回整型值0,其他值表示失败。\r
+* 示例\r
+```python\r
+    import slip\r
+    ret=slip.construct(UART.UART2, slip.SLIP_INNER, 0)\r
+```\r
+#### 获取slip网卡ip信息\r
+>    **slipInfo=slip.ipconfig()**\r
+\r
+获取slip网卡ip信息\r
+* 参数  \r
+无  \r
+* 返回值    \r
+  \r
+|  参数   | 类型  |说明|\r
+|  ----  | ----  | ---|\r
+| slipInfo  | tuple | -1或者tuple类型的ip信息 |\r
+* slipInfo参数说明\r
+\r
+|  序号   | 类型 | 说明 |\r
+|  ----  | ---- | ---- | \r
+| 0  | string | slip网卡ip |\r
+| 1  | string | slip网卡子网掩码 |\r
+| 1  | string | slip网卡网关|\r
+| 1  | int | mtu信息|\r
+* 示例\r
+```python\r
+    import slip\r
+    slipInfo=slip.ipconfig()\r
+    print(slipInfo)\r
+```\r
+#### 移除slip网卡\r
+> **slip.destroy()**\r
+\r
+移除slip网卡  \r
+* 参数  \r
+无\r
+* 返回值  \r
+无\r
+* 示例\r
+```python\r
+    import slip\r
+    slip.destroy()\r
+```\r
+###设置默认网卡\r
+> **slip.set_default_netif(ip_str)**\r
+\r
+通过ip信息设置默认网卡\r
+* 参数\r
+\r
+|  参数   | 类型|说明|\r
+|  ----  | ---- | ---|\r
+| ip_str | string | 网卡ip信息 |\r
+\r
+* 返回值  \r
+成功返回整型值0,其他值表示失败。\r
+* 示例\r
+```python\r
+    import slip\r
+    ip_str='192.168.1.10'\r
+    slip.set_default_netif(ip_str)\r
+```\r
+\r
+#### slip路由添加\r
+> **slip.router_add(ip_str, netmask_str)**\r
+\r
+添加slip网卡路由信息,注意这部分只有在使用slip时生效  \r
+* 参数\r
+\r
+|  参数   | 类型  |说明|\r
+|  ----  | ----  | ---|\r
+| ip_str  | string | 设置slip万卡转发的ip |\r
+| netmask_str  | string | 设置对应的子网掩码 |\r
+\r
+* 返回值  \r
+成功返回整型值0,其他值表示失败。\r
+* 示例\r
+```python\r
+    import slip\r
+    ip_str='192.168.1.0'\r
+    netmask_str='255.255.255.0'\r
+    slip.router_add(ip_str, netmask_str)\r
+```\r
+\r
+#### slip路由删除\r
+> **slip.router_add(ip_str, netmask_str)**\r
+\r
+删除slip网卡路由信息,注意这部分只有在使用slip时生效\r
+* 参数\r
+\r
+|  参数   | 类型  |说明|\r
+|  ----  | ----  | ---|\r
+| ip_str  | string | 设置slip万卡转发的ip |\r
+| netmask_str  | string | 设置对应的子网掩码 |\r
+\r
+* 返回值  \r
+  成功返回整型值0,其他值表示失败。\r
+* 示例\r
+```python\r
+    import slip\r
+    ip_str='192.168.1.0'\r
+    netmask_str='255.255.255.0'\r
+    slip.router_delete(ip_str, netmask_str)\r
+```\r
+\r
+####使用示例\r
+```python\r
+import slip\r
+import dataCall\r
+import utime\r
+from machine import UART\r
+\r
+\r
+def wait_datacall_success():\r
+    global lteInfo\r
+    print('waiting datacall success...')\r
+    while 1:\r
+        lteInfo = dataCall.getInfo(1, 0)\r
+        if lteInfo != -1:\r
+            break\r
+        utime.sleep(2)\r
+    print('success get dataCall info: ')\r
+    print(lteInfo)\r
+\r
+def slip_start():\r
+    wait_datacall_success()\r
+    # 创建slip网卡\r
+    ret=slip.construct(UART.UART2, slip.SLIP_INNER, 0)\r
+    if ret != 0:\r
+        print('slip netif construct fail')\r
+        return -1\r
+    # 获取slip的网络配置\r
+    slipInfo=slip.ipconfig()\r
+    print('slip ipconfig: ')\r
+    print(slipInfo)\r
+    if lteInfo != -1:\r
+        # 设置默认网卡,当设置slip作为上网卡时不设置该接口,即SLIP_OUTER类型\r
+        ret = slip.set_default_netif(lteInfo[2][2])\r
+        if ret != 0:\r
+            print('slip set default netif fail')\r
+            return -1\r
+    # 添加路由信息,设置网卡转发规则192.168.4.0表示ap的网段,255.255.255.0子网掩码\r
+    ret = slip.router_add('192.168.4.0', '255.255.255.0')\r
+    if ret != 0:\r
+        print('slip add subnet routers fail')\r
+        return -1\r
+    return 0\r
+def slip_stop():\r
+    slip.destroy()\r
+\r
+err = slip_start()\r
+if err == 0:\r
+    print('slip network card create success')\r
+else:\r
+    print('slip network card create fail')\r
+```
\ No newline at end of file
index 48529ea..e580d4f 100644 (file)
@@ -6,21 +6,28 @@ from pathlib import Path
 
 def run_git_command(command):
     try:
+        # 设置环境变量以正确处理中文文件名
+        env = os.environ.copy()
+        env['LC_ALL'] = 'C.UTF-8'
+        env['LANG'] = 'C.UTF-8'
+        
         result = subprocess.run(
             command,
             stdout=subprocess.PIPE,
             stderr=subprocess.PIPE,
             encoding='utf-8',
             errors='replace',
-            check=True
+            check=True,
+            env=env
         )
         return result.stdout.strip()
     except subprocess.CalledProcessError as e:
         print(f"Error executing git command: {e.stderr}")
-        sys.exit(1)
+        # 不要立即退出,让调用者处理错误
+        raise
     except UnicodeDecodeError as e:
         print(f"Encoding error: {e}")
-        sys.exit(1)
+        raise
 
 def main():
     if len(sys.argv) > 3:
@@ -198,9 +205,24 @@ def main():
                     dst.write(src.read())
             else:
                 # 从暂存区提取文件内容
-                blob_content = run_git_command(
-                    ["git", "show", f":{file_path}"]
-                )
+                # 对于包含特殊字符的文件名,使用更安全的方式
+                try:
+                    blob_content = run_git_command(
+                        ["git", "show", f":{file_path}"]
+                    )
+                except:
+                    # 如果pathspec失败,尝试使用git cat-file
+                    try:
+                        # 获取文件的blob hash
+                        blob_hash = run_git_command(
+                            ["git", "ls-files", "--stage", file_path]
+                        ).split()[1]
+                        blob_content = run_git_command(
+                            ["git", "cat-file", "blob", blob_hash]
+                        )
+                    except:
+                        print(f"Warning: Could not extract staged content for {file_path}")
+                        continue
                 with open(target_file, "w", encoding='utf-8') as dst:
                     dst.write(blob_content)
         except Exception as e: