Git Updater Script

Post Reply
bell07
Member
Posts: 604
Joined: Sun Sep 04, 2016 15:15
GitHub: bell07

Git Updater Script

by bell07 » Post

Since I use mods, I install them most from git repositories. I like maintained software and I do not like to do it by self :-)
To get all the repositories in sync I am to lazy, so I has written an update script for me.

The script does work on linux, I use it with bash shell. Maybe it is usable on windows with cygwin-bash, but I did not tested it, I do not use the W$.

The script does search from current directory trough all sub-directories for git-managed repos (".git" exists) and take the git-magic. Initial working support for ".hg" is included, but I do not have currently any mercurial repos.

How to use:

Code: Select all

cd ~/.minetest
sh update.sh 9
The number is log-level. 0-3 is implemented, but I use mostly 9 or 99 on command line, don't ask me why .

The script:

Code: Select all

~/.minetest $ cat update.sh 

Code: Select all

#!/bin/bash

VERBOSE="$1"

if  [ "$VERBOSE" == "" ]; then
   VERBOSE=0
fi

# 0: just update relevant infos
# 1 display "branch -vaa" + local changes
# 2 display log on current branch
# 3 display "remote -v"


find . -name ".git" -type d | while read repo; do
   echo ''
   echo '--------------------------------------'
   echo "$(dirname "$repo")"
   echo '--------------------------------------'

   cd "$(dirname "$repo")"

# fetch all connected remotes
   for repo in $(git remote); do
      git fetch "$repo" --prune
   done

# Display repo status in different verbosity
   if [ "$VERBOSE" -ge 1 ]; then
      git branch -vva
   fi

# create log range
   currentbranch="$(git branch | grep '^*' | sed 's:\* ::g')"
   remotebranch="$(git branch -avv | grep '^\*' | sed 's@.*\[@@g;s@[]:].*@@g' | grep -v '^\*')"

# print the log information
   if [ "$VERBOSE" -ge 2 ]; then
      if [ "$remotebranch" == "" ]; then
         echo '!!!!! No remote branch for' $currentbranch '!!!!!!'
      else
         echo $currentbranch '=>' "$remotebranch"
         git --no-pager log  $currentbranch..$remotebranch
      fi

      if [ "$VERBOSE" -ge 3 ]; then
         git remote -v
      fi
   fi

# Now pull+merge
#   git pull. (from man git-pull: git pull is shorthand for git fetch followed by git merge FETCH_HEAD.)
   if [ ! "$remotebranch" == "" ]; then
      git merge "$remotebranch"
   fi

# display local changes
   if [ $VERBOSE -ge 1 ]; then
	   git --no-pager diff #display local changes
   fi

   echo "last commit: $(git log -1 --format=%cd)"

   cd - >/dev/null
done

#########################################
# Find all ".hg" and pull them
find . -name ".hg" -type d | while read repo; do
   echo "$(dirname "$repo")"
   cd "$(dirname "$repo")"
   hg pull
   cd - >/dev/null
done
Last edited by bell07 on Fri Apr 28, 2017 07:22, edited 1 time in total.

User avatar
Linuxdirk
Member
Posts: 3218
Joined: Wed Sep 17, 2014 11:21
In-game: Linuxdirk
Location: Germany
Contact:

Re: Git Updater Script

by Linuxdirk » Post

bell07 wrote:To get all the repositories in sync I am to lazy, so I has written an update script for me.
I did the same. Not as complex as yours and no automatic detection (because I have Git managed stuff there I don’t want to be updated because I am the maintainer by myself).

Code: Select all

#! /usr/bin/env luajit

local basedir = '/home/USERNAME/.minetest/mods/'

local mods = {
    'mod_1',
    'mod_2',
    'mod_3',
    'mod_N'
}

print('Updating '..#mods..' mods\n')

for i,mod in pairs(mods) do
    print('---------------------------------------------\n'..'Processing '..mod)
    os.execute('git -C '..basedir..mod..' pull')
    print('---------------------------------------------\n')
end
Just change basedir to where your mods are located, add your mods to the mods table and run the script.

bell07
Member
Posts: 604
Joined: Sun Sep 04, 2016 15:15
GitHub: bell07

Re: Git Updater Script

by bell07 » Post

Linuxdirk wrote:because I have Git managed stuff there I don’t want to be updated because I am the maintainer by myself
The great at the Git is it can handle this, from there my script can handle it too.
The script updates the repositories with remote branch (--set-upstream-to=) is set. So
- for current development if you work as usual on a local development branch, the script does not touch them.
- If you are maintainer and the sole contributor you get of course just no updates.
- If you take the changed directly on the master (like change rarity settings) you need to take a local commit for this. In this case all upstream changes will be merged without any conflicts in most cases.

I use the script on my growing mod collection once per day, mostly without any issues.

bell07
Member
Posts: 604
Joined: Sun Sep 04, 2016 15:15
GitHub: bell07

Re: Git Updater Script

by bell07 » Post

Small update: added "--prune" option to "git fetch" to clean up removed remote branches.

Merak
Member
Posts: 116
Joined: Sat Nov 05, 2016 20:34

Re: Git Updater Script

by Merak » Post


bell07
Member
Posts: 604
Joined: Sun Sep 04, 2016 15:15
GitHub: bell07

Re: Git Updater Script

by bell07 » Post

My "Rollout" script in addition: pull all repos on 1 system and then copy it to other systems. The git history is not copied to other systems.

Code: Select all

#!/bin/sh
RSYNC_PARAM="--recursive --links --safe-links --perms --times"
RSYNC_PARAM="$RSYNC_PARAM -v --omit-dir-times --compress --force --whole-file --delete"
RSYNC_PARAM="$RSYNC_PARAM --stats --human-readable --timeout=180"
RSYNC_PARAM="$RSYNC_PARAM --exclude [.]git/"
MT_REMOTE="bell07@minetestserver:~/.minetest"
MT_LOCAL="$HOME/.minetest"
rsync $RSYNC_PARAM "$MT_REMOTE"/mods  "$MT_REMOTE"/games "$MT_LOCAL"

sreq
New member
Posts: 1
Joined: Fri Jul 21, 2017 17:13

Re: Git Updater Script

by sreq » Post

Code: Select all

import os

mpath = os.path.expanduser("~")+"/.minetest"
mmods = os.path.join(mpath, "mods")
mclientmods = os.path.join(mpath, "clientmods")
mgames = os.path.join(mpath, "games")

def getGit(path, c):
	r = ""
	g = os.popen("git -C "+path+" "+c, "r")
	while 1:
		line = g.readline()
		if not line: break
		r+=line
	g.close()
	return r[:-1]

def UpdateDir(path):
	if not os.path.exists(path):
		os.makedirs(path)
	for file in os.listdir(path):
		dpath = os.path.join(path, file)
		if(os.path.isdir(os.path.join(dpath, ".git"))):
			r = getGit(dpath, "remote")
			os.system("git -C "+dpath+" pull "+r+" master")
UpdateDir(mgames)
UpdateDir(mmods)
UpdateDir(mclientmods)

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests