简体   繁体   English

如何更新git repo或创建如果没有这样的repo

[英]How to update git repo or to create if there is no such repo

I want a script that will update the git repo. 我想要一个将更新git repo的脚本。 I want it to update the repo if the folder with the repo exists and I want to clone the repo if there is no such folder. 如果存在具有repo的文件夹,我希望它更新repo,如果没有这样的文件夹,我想克隆repo。

I want to specify 2 thing for that script: 我想为该脚本指定2件事:

  • git remote url (for example ssh://git.example.com/var/git/repo.git ) git remote url(例如ssh://git.example.com/var/git/repo.git
  • forder where the git repo should be (for example /var/lib/git/repo ) 应该是git repo的forder(例如/var/lib/git/repo

It is not very hard to write this kind of script, but I think that this task is something very common and it is already solved. 编写这种脚本并不是很难,但我认为这个任务很常见,已经解决了。

The task is pretty simple, but there are some things that should be done carefully. 任务很简单,但有些事情应该仔细完成。 For example, the main branch of the repo can not the master, but something else, the scrip should give the non zero exit status in case of error, it should work if there is some changes in the repo, and so on. 例如,repo的主分支不能是master,而是其他东西,如果出现错误,脚本应该给出非零退出状态,如果repo中有一些变化,它应该有效,依此类推。

So my question — what script can I use to carefully solve the task of cloning or updating the repo. 所以我的问题 - 我可以用什么脚本来仔细解决克隆或更新回购的任务。

Here is simple ruby code 这是简单的ruby代码

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?

If I quickly had to do this, I would try node because it offers some good libraries for git integration and I think it's now a bit easier than bash if you're not used to any of them. 如果我很快就必须这样做,我会尝试节点,因为它为git集成提供了一些好的库,我认为现在比bash更容易,如果你不习惯它们中的任何一个。 The script would do the following: 该脚本将执行以下操作:

1) Navigation to the folder and do a simple git status , any command would do, becauswe if it's not a git repo it will say so: 1)导航到文件夹并执行一个简单的git status ,任何命令都可以,因为如果它不是git repo它会这样说:

fatal: Not a git repository

1.a) If it's not a git repo a simple "git clone url" does the trick and the script exists 1.a)如果它不是一个git repo,那么简单的“git clone url”就可以完成这个技巧并且脚本存在

2.a) Ok, so it there is not an error, the git status will give you either a clean branch with no changes or it will tell you have changes 2.a)好的,所以它没有错误,git状态会给你一个没有变化的干净分支,或者它会告诉你有变化

2.aa) If you want to update now run a "git pull --ff-only" which will prevent unwanted merges. 2.aa)如果你想立即更新,请运行“git pull --ff-only”,这将防止不必要的合并。 If this fails, then I think you should solve the problems manually. 如果失败,那么我认为你应该手动解决问题。

2.ab) If you have local changes I would exit the script as well. 2.ab)如果您有本地更改,我也会退出脚本。

Libraries to use: 要使用的库:

I like gift but there are many others if you look into npm. 我喜欢礼物但是如果你看看npm还有很多其他礼物 In case you don't want to follow my suggestion a bash script will do as well. 如果您不想遵循我的建议,bash脚本也会这样做。

Hope this helped. 希望这有帮助。

Van also be run on a Build Server 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

IF run on a Build Server, you Couleur appen git Status. 如果在构建服务器上运行,您可以使用Couleur appen git Status。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM