- def update(self):
- isBranch = (self.commit.startswith('origin/'))
- if isBranch:
- branchname = self.commit[len('origin/'):]
- else:
- branchname = "tag"
- # get us a git repository, and the "origin" remote
- if os.path.exists(self.folder):
- # load existing repo
- repo = git.Repo(self.folder)
- origin = repo.remotes.origin
- else:
- # create a new one
- os.makedirs(self.folder)
- repo = git.Repo.init(self.folder)
- origin = repo.create_remote('origin', self.url)
- origin.fetch(progress=Git._ProgressPrinter()) # download new data
- print " "*80+"\r", # clean the line we are in
- # create/find correct branch
- if branchname in repo.heads:
- branch = repo.heads[branchname]
- else:
- branch = repo.create_head(branchname, self.commit)
- if isBranch:
- branch.set_tracking_branch(origin.refs[branchname])
- # update it to the latest remote commit
- branch.checkout()
- repo.git.rebase(self.commit)
- print "...done",
- if repo.head.reference.commit != repo.refs[self.commit].commit:
- print "(keeping local patches around)",
- print
+ def update(self, mode = MODE_REBASE):
+ isBranch = (self.commit.startswith('origin/'))
+ if isBranch:
+ branchname = self.commit[len('origin/'):]
+ else:
+ branchname = "tag"
+ # get us a git repository, and the "origin" remote
+ if os.path.exists(self.folder):
+ # load existing repo
+ os.chdir(self.folder)
+ git.remote("set-url", "origin", self.url) # make sure we use the current URL
+ else:
+ # create a new one
+ os.makedirs(self.folder)
+ os.chdir(self.folder)
+ git.init()
+ git.remote("add", "origin", self.url)
+ git.fetch("origin")
+ if mode == MODE_FETCH:
+ return
+ # create/find correct branch
+ if not git.branch("--list", branchname): # the branch does not yet exit
+ git.branch(branchname, self.commit)
+ if isBranch: # make sure we track the correct remote branch
+ git.branch("-u", self.commit, branchname)
+ # update it to the latest remote commit
+ git.checkout(branchname, get_stderr=True)
+ if mode == MODE_RESET:
+ git.reset("--hard", self.commit)
+ else:
+ git.rebase(self.commit)
+ # update submodules
+ git.submodule("update", "--init", "--recursive", "--rebase")
+ # done
+ print("...done", end=' ')
+ if git.rev_parse("HEAD") != git.rev_parse(self.commit):
+ print("(keeping local patches around)", end=' ')
+ print()