-
Notifications
You must be signed in to change notification settings - Fork 16
/
git-sync
executable file
·56 lines (46 loc) · 1.41 KB
/
git-sync
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python
#
# git-sync
#
# Script for syncing the main branch with origin and rebasing changes in the
# current branch on top of the new main.
#
# Usage:
# $ git sync [-i]
#
# Is equivalent to calling:
# main=$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')
# git checkout $main
# git pull origin $main
# git checkout feature-branch
# git rebase $main feature-branch [-i]
#
# Reasonable efforts are taken to handle errors and make sure you end off back
# on your initial branch.
import sys
import os
import git
import util
if len(sys.argv) > 2:
util.fatal('Incorrect usage, missing branch name. Use: git sync [-i]')
interactive = False
if len(sys.argv) > 1:
if sys.argv[1] == '-i':
interactive = True
else:
util.fatal('Incorrect usage, unexpected argument to git sync: "%s"' % sys.argv[1])
try:
repo = git.Repo(os.getcwd(), search_parent_directories=True)
except git.exc.InvalidGitRepositoryError:
util.fatal('git sync must be run from within a valid git repository.')
util.fatal_if_dirty(repo)
initial_branch = repo.active_branch
util.update_main(repo, initial_branch)
main = util.main_branch_name(repo)
util.info('Rebasing %s on %s' % (initial_branch, main))
initial_branch.checkout()
if interactive:
repo.git.rebase(main, '-i')
else:
repo.git.rebase(main)
util.success('%s synced with origin, %s rebased' % (main, initial_branch))