import os
import subprocess as s

pushed = os.environ.get('branch','pushed')

def main():
	for f in os.listdir('.'):
		if os.path.exists(os.path.join(f,'.git')):
			print(f)
			os.chdir(f)
			found_pushed = False
			pid = s.Popen(['git','branch'],stdout=s.PIPE)
			for line in pid.stdout:
				line = line.decode('utf-8')
				line = line.strip(' *\t\n')
				if line == pushed:
					found_pushed = True
					break
			pid.wait()
			if found_pushed:
				merge()
			os.chdir('..')
def merge():
	s.check_call(['git','merge',pushed])
	s.call(['git','branch','-d',pushed])

main()
