bash 脚本中的 git 命令在第一个之后运行时出现问题

bash 脚本中的 git 命令在第一个之后运行时出现问题

我正在努力使用一个脚本来备份根文件夹中的配置文件,该脚本将文件复制到 myuser 目录中的文件夹内,复制原始路径结构,将文件添加到 git 存储库并提交更改。该脚本采用要备份的配置文件的路径作为参数:

BU.sh /etc/apt/sources.list

显然,程序的第一次运行在仅包含 README.md 的新克隆存储库上正常工作,但是在接下来的运行中,git 命令似乎没有任何效果。

脚本是这样的(添加了一些调试信息):

#! /bin/bash
#
set -x -e -u
export GIT_DIR=/home/myuser/config-BU/.git
export GIT_WORK_TREE=/home/myuser/
        #echo "file NOT in USER DIR, is NOT symlink";
        ls -l config-BU
        mkdir -p /home/myuser/config-BU/"${1%/*}";
        ls -l config-BU
        sudo install -o myuser -g myuser -m 660 "$1" "/home/myuser/config-BU$(realpath $1)";
        ls -l config-BU
        sudo -k;
        git status
        git ls-files
        git add "/home/myuser/config-BU$(realpath $1)";
        git ls-files
        git commit -am "adding file inside BU autom: file NOT in user folder" -m "$(realpath $1)";
        git log

两次运行该命令的结果是:

MYPROMPT>:~$ ./cp2bu2_badpart.sh /var/log/faillog
+ export GIT_DIR=/home/myuser/config-BU/.git
+ GIT_DIR=/home/myuser/config-BU/.git
+ export GIT_WORK_TREE=/home/myuser/
+ GIT_WORK_TREE=/home/myuser/
+ ls -l config-BU
total 4
-rw-r--r-- 1 myuser myuser 27 Jul  7 19:40 README.md
+ mkdir -p /home/myuser/config-BU//var/log
+ ls -l config-BU
total 8
-rw-r--r-- 1 myuser myuser   27 Jul  7 19:40 README.md
drwxr-xr-x 3 myuser myuser 4096 Jul  7 22:25 var
++ realpath /var/log/faillog
+ sudo install -o myuser -g myuser -m 660 /var/log/faillog /home/myuser/config-BU/var/log/faillog
[sudo] password for myuser:
+ ls -l config-BU
total 8
-rw-r--r-- 1 myuser myuser   27 Jul  7 19:40 README.md
drwxr-xr-x 3 myuser myuser 4096 Jul  7 22:25 var
+ sudo -k
+ git status
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
deleted:    README.md

Untracked files:
  (use "git add <file>..." to include in what will be committed)

==LIST OF FILES IN MY HOME DIR==
no changes added to commit (use "git add" and/or "git commit -a")
+ git ls-files
README.md
++ realpath /var/log/faillog
+ git add /home/myuser/config-BU/var/log/faillog
+ git ls-files
README.md
++ realpath /var/log/faillog
+ git commit -am 'adding file inside BU autom: file NOT in user folder' -m /var/log/faillog
[main 2487ec3] adding file inside BU autom: file NOT in user folder
 1 file changed, 2 deletions(-)
 delete mode 100644 README.md
+ git log
commit 2487ec3857d355313971f2a45ba7d318870bc219 (HEAD -> main)
Author: myuser <myemail>
Date:   Fri Jul 7 22:26:41 2023 +0200

    adding file inside BU autom: file NOT in user folder
   
    /var/log/faillog

commit 84112df2c48137f247af78497e416182f0e0c24c (origin/main, origin/HEAD)
Author: Pippetta87 <[email protected]>
Date:   Thu Oct 13 03:59:13 2022 +0200

    Create README.md

MYPROMPT>:~$ ./cp2bu2_badpart.sh /etc/X11/rgb.txt
+ export GIT_DIR=/home/myuser/config-BU/.git
+ GIT_DIR=/home/myuser/config-BU/.git
+ export GIT_WORK_TREE=/home/myuser/
+ GIT_WORK_TREE=/home/myuser/
+ ls -l config-BU
total 8
-rw-r--r-- 1 myuser myuser   27 Jul  7 19:40 README.md
drwxr-xr-x 3 myuser myuser 4096 Jul  7 22:25 var
+ mkdir -p /home/myuser/config-BU//etc/X11
+ ls -l config-BU
total 12
-rw-r--r-- 1 myuser myuser   27 Jul  7 19:40 README.md
drwxr-xr-x 3 myuser myuser 4096 Jul  7 22:27 etc
drwxr-xr-x 3 myuser myuser 4096 Jul  7 22:25 var
++ realpath /etc/X11/rgb.txt
+ sudo install -o myuser -g myuser -m 660 /etc/X11/rgb.txt /home/myuser/config-BU/etc/X11/rgb.txt
[sudo] password for myuser:
+ ls -l config-BU
total 12
-rw-r--r-- 1 myuser myuser   27 Jul  7 19:40 README.md
drwxr-xr-x 3 myuser myuser 4096 Jul  7 22:27 etc
drwxr-xr-x 3 myuser myuser 4096 Jul  7 22:25 var
+ sudo -k
+ git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)

==LIST OF FILES IN MY HOME DIR==

nothing added to commit but untracked files present (use "git add" to track)
+ git ls-files
++ realpath /etc/X11/rgb.txt
+ git add /home/myuser/config-BU/etc/X11/rgb.txt
+ git ls-files
++ realpath /etc/X11/rgb.txt
+ git commit -am 'adding file inside BU autom: file NOT in user folder' -m /etc/X11/rgb.txt
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)

==LIST OF FILES IN MY HOME DIR==

nothing added to commit but untracked files present (use "git add" to track)

正如您所看到的,第二次运行不会将文件添加到 git 存储库,并且最后一个 git 日志不会执行:之后的 git 命令git status似乎被忽略。

此致

答案1

你可以这样开始你的脚本:

export GIT_DIR=/home/myuser/config-BU/.git
export GIT_WORK_TREE=/home/myuser/

你的工作目录实际上应该是/home/myuser/config-BU.

这就是为什么当你运行时git status你会看到:

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
deleted:    README.md

README.md它在您的GIT_WORK_TREE( ) 中找不到/home/myuser/README.md,因此认为该文件已被删除。

然后,当您提交更改时,您会再次看到它README.md从索引中删除:

git commit -am 'adding file inside BU autom: file NOT in user folder' -m /var/log/faillog
[main 2487ec3] adding file inside BU autom: file NOT in user folder
 1 file changed, 2 deletions(-)
 delete mode 100644 README.md

相关内容