1

我想要一个可以更新 git repo 的脚本。如果包含 repo 的文件夹存在,我希望它更新 repo,如果没有这样的文件夹,我想克隆 repo。

我想为该脚本指定两件事:

  • git 远程网址(例如ssh://git.example.com/var/git/repo.git
  • git repo 应该在哪里(例如/var/lib/git/repo

写这种脚本不是很难,但我认为这个任务很常见,已经解决了。

任务很简单,但有些事情应该小心完成。例如,repo 的主分支不能是 master,而是其他东西,脚本应该在错误的情况下给出非零退出状态,如果 repo 有一些变化,它应该可以工作,等等。

所以我的问题是——我可以使用什么脚本来仔细解决克隆或更新 repo 的任务。

4

3 回答 3

3

这是简单的红宝石代码

REPO_PATH = '/Users/full_path/to_repo'
REPO_NAME = 'xxx'
GITHUB_URL = 'git@github.com:xxx/xxx.git'   

def change_dir_to_repo
  Dir.chdir(REPO_PATH)
  puts system('pwd')
end   

def git_repo_exists?
  if Dir.exists?(REPO_NAME)
    puts "Git repo #{REPO_NAME} exists."
    update_git_repo
  else
    puts "Git repo #{REPO_NAME} does not exists."
    clone_git_repo
  end
end   

def clone_git_repo
  system("git clone #{GITHUB_URL}")
  puts "Done"
end  

def update_git_repo
  puts "Changing directory to #{REPO_NAME}"
  Dir.chdir(REPO_NAME)
  puts "Changing branch to master"
  system('git checkout master')
  puts "updating git repo"
  system('git pull')
  puts "Done"
end  

change_dir_to_repo
git_repo_exists?
于 2013-09-13T11:38:40.350 回答
1

如果我必须尽快这样做,我会尝试使用 node,因为它提供了一些用于 git 集成的优秀库,而且我认为如果你不习惯其中任何一个,它现在比 bash 更容易一些。该脚本将执行以下操作:

1)导航到文件夹并做一个简单的git status,任何命令都可以,因为如果它不是一个git repo,它会这样说:

fatal: Not a git repository

1.a)如果它不是一个 git repo,一个简单的“git clone url”就可以了,并且脚本存在

2.a)好的,所以没有错误,git状态会给你一个没有变化的干净分支,或者它会告诉你有变化

2.aa) 如果您想立即更新,请运行“git pull --ff-only”,这将防止不必要的合并。如果失败,那么我认为您应该手动解决问题。

2.ab)如果您有本地更改,我也会退出脚本。

使用的库:

我喜欢礼物,但如果你研究 npm,还有很多其他的。如果您不想遵循我的建议,那么 bash 脚本也可以。

希望这有帮助。

于 2013-09-13T10:58:02.397 回答
0

Van 也可以在 Build Server 上运行

#peventing failure  
mkdir $rep || true
#succeeds always
cd $rep
#one of them will do the job!
git clone $REMOTE_URL . || git pull

如果在 Build Server 上运行,则 Couleur 会添加 git 状态。

于 2015-06-01T23:18:43.287 回答