summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThibault Saunier <tsaunier@gnome.org>2016-08-08 13:05:01 -0400
committerThibault Saunier <tsaunier@gnome.org>2016-08-10 12:14:58 -0400
commitb4c2d0a1fa26260a6091adeb6aaaed486bbd3ac3 (patch)
tree6403f32680e41ca19355b0af3ec03374546c1e46
parentefb804231d63475ebc45a9098465aef28d92bea4 (diff)
Remove checkout and make fetch behave like git fetch
But add a `--checkout` argument to `fetch` so that the user can still get the branch almost verbatim to what has been proposed in one step. Reviewed-by: Daniel Stone <daniels@collabora.com> Differential Revision: https://phabricator.freedesktop.org/D1259
-rwxr-xr-xgit-phab107
1 files changed, 83 insertions, 24 deletions
diff --git a/git-phab b/git-phab
index eabc6aa..4d4799c 100755
--- a/git-phab
+++ b/git-phab
@@ -353,7 +353,7 @@ Paste API Token from that page and press <enter>: """ % self.phabricator_uri)
353 # Fetch what has already been proposed on the task if we don't have 353 # Fetch what has already been proposed on the task if we don't have
354 # it locally yet. 354 # it locally yet.
355 if not remote_commit: 355 if not remote_commit:
356 remote_commit = self.fetch()[0] 356 remote_commit = self.fetch_from_task()[0]
357 357
358 # Get the index in commits and all_commits lists of the common 358 # Get the index in commits and all_commits lists of the common
359 # ancestor between HEAD and what has already been proposed. 359 # ancestor between HEAD and what has already been proposed.
@@ -747,8 +747,9 @@ Paste API Token from that page and press <enter>: """ % self.phabricator_uri)
747 def run_linter(self): 747 def run_linter(self):
748 if not os.path.exists(".pre-commit-config.yaml"): 748 if not os.path.exists(".pre-commit-config.yaml"):
749 if os.path.exists(".arclint"): 749 if os.path.exists(".arclint"):
750 return subprocess.check_output("arc lint --never-apply-patches", 750 return subprocess.check_output(
751 shell=True).decode("utf-8") 751 "arc lint --never-apply-patches",
752 shell=True).decode("utf-8")
752 else: 753 else:
753 return None 754 return None
754 command = ["pre-commit", "run", "--files"] 755 command = ["pre-commit", "run", "--files"]
@@ -1509,7 +1510,7 @@ Paste API Token from that page and press <enter>: """ % self.phabricator_uri)
1509 commits = self.get_commits(self.revision_range) 1510 commits = self.get_commits(self.revision_range)
1510 self.print_commits(commits) 1511 self.print_commits(commits)
1511 1512
1512 def fetch(self): 1513 def fetch_from_task(self):
1513 1514
1514 reply = self.phabricator.maniphest.query(ids=[int(self.task[1:])]) 1515 reply = self.phabricator.maniphest.query(ids=[int(self.task[1:])])
1515 1516
@@ -1527,13 +1528,77 @@ Paste API Token from that page and press <enter>: """ % self.phabricator_uri)
1527 print("Commit '%s' from remote branch '%s' has been fetched" % 1528 print("Commit '%s' from remote branch '%s' has been fetched" %
1528 (commit.hexsha, branch)) 1529 (commit.hexsha, branch))
1529 1530
1530 return (commit, branch) 1531 return (commit, remote, branch)
1532
1533 def create_fake_fetch(self, revision, diff):
1534 current_branch = self.repo.active_branch
1535 base_commit = diff.get("sourceControlBaseRevision")
1536 if base_commit:
1537 try:
1538 self.repo.git.checkout(base_commit)
1539 except git.exc.GitCommandError:
1540 base_commit = None
1541
1542 if not base_commit:
1543 print("%sWARNING: Building `fake fetch` from"
1544 " current commit (%s)\nas we do not have"
1545 " information or access to the base commit"
1546 " the revision has been proposed from%s" % (
1547 Colors.WARNING, self.repo.head.commit.hexsha,
1548 Colors.ENDC))
1549 self.repo.git.checkout(self.repo.head.commit.hexsha)
1550
1551 pq = self.get_differentials_to_apply_for_revision()
1552 if pq:
1553 n = 0
1554 while pq != []:
1555 (r, d) = pq.pop()
1556
1557 filename = self.write_patch_file(r, d)
1558 print("Applying D{}".format(r['id']))
1559
1560 self.am_patch(filename, None)
1561 os.unlink(filename)
1562 n += 1
1563
1564 branch_name = revision.get('branch')
1565 if not branch_name:
1566 branch_name = self.differential
1567 else:
1568 branch_name = self.differential + "-" + branch_name
1569
1570 remote = "file://" + self.repo.working_dir
1571 with open(os.path.join(self.repo.working_dir, ".git",
1572 "FETCH_HEAD"),
1573 "w") as fetch_head_file:
1574 fetch_head_file.write("%s branch '%s' of file://%s" % (
1575 self.repo.head.commit.hexsha, branch_name, remote))
1576
1577 current_branch.checkout()
1578
1579 return remote, branch_name
1531 1580
1532 def do_fetch(self): 1581 def do_fetch(self):
1533 if not self.task: 1582 if not self.differential and not self.task:
1534 self.die("No task provided. Aborting.") 1583 self.die("No task or revision provided. Aborting.")
1584
1585 if self.checkout:
1586 self.do_checkout()
1587 return
1588
1589 if self.differential:
1590 infos = self.fetch_from_revision()
1591 if not isinstance(infos[0], git.Commit):
1592 remote, branch_name = self.create_fake_fetch(*infos)
1593 else:
1594 commit, branch_name = infos
1595 remote = self.staging_url
1596 else:
1597 commit, remote, branch_name = self.fetch_from_task()
1535 1598
1536 self.fetch() 1599 print("From %s\n"
1600 " * branch %s -> FETCH_HEAD" % (
1601 remote, branch_name))
1537 1602
1538 def fetch_from_revision(self): 1603 def fetch_from_revision(self):
1539 did = self.differential.strip("D") 1604 did = self.differential.strip("D")
@@ -1541,19 +1606,16 @@ Paste API Token from that page and press <enter>: """ % self.phabricator_uri)
1541 revision, diff = self.get_revision_and_diff(diff=did) 1606 revision, diff = self.get_revision_and_diff(diff=did)
1542 1607
1543 if not self.fetch_staging_commits(diff): 1608 if not self.fetch_staging_commits(diff):
1544 return None, None 1609 return revision, diff
1545 1610
1546 return (self.repo.rev_parse("FETCH_HEAD"), 1611 return (self.repo.rev_parse("FETCH_HEAD"),
1547 self.differential + '-' + revision['branch']) 1612 self.differential + '-' + revision['branch'])
1548 1613
1549 def do_checkout(self): 1614 def do_checkout(self):
1550 if not self.differential and not self.task:
1551 self.die("No task or revision provided. Aborting.")
1552
1553 pq = None 1615 pq = None
1554 if self.differential: 1616 if self.differential:
1555 commit, remote_branch_name = self.fetch_from_revision() 1617 infos = self.fetch_from_revision()
1556 if commit: 1618 if not isinstance(infos[0], git.Commit):
1557 branchname_match_method = self.revision_from_branchname 1619 branchname_match_method = self.revision_from_branchname
1558 branch_name = self.differential 1620 branch_name = self.differential
1559 else: 1621 else:
@@ -1576,7 +1638,7 @@ Paste API Token from that page and press <enter>: """ % self.phabricator_uri)
1576 branchname_match_method = self.revision_from_branchname 1638 branchname_match_method = self.revision_from_branchname
1577 remote_branch_name = branch_name = self.differential 1639 remote_branch_name = branch_name = self.differential
1578 else: 1640 else:
1579 commit, remote_branch_name = self.fetch() 1641 commit, remote, remote_branch_name = self.fetch_from_task()
1580 branchname_match_method = self.task_from_branchname 1642 branchname_match_method = self.task_from_branchname
1581 branch_name = self.task 1643 branch_name = self.task
1582 1644
@@ -1682,7 +1744,7 @@ Paste API Token from that page and press <enter>: """ % self.phabricator_uri)
1682 self.die("Repository is dirty. Aborting.") 1744 self.die("Repository is dirty. Aborting.")
1683 1745
1684 if self.task: 1746 if self.task:
1685 commit, remote_branch_name = self.fetch() 1747 commit, remote, remote_branch_name = self.fetch_from_task()
1686 branch = self.repo.active_branch 1748 branch = self.repo.active_branch
1687 if not self.prompt("Do you want to reset branch %s to %s?" % 1749 if not self.prompt("Do you want to reset branch %s to %s?" %
1688 (branch.name, commit.hexsha)): 1750 (branch.name, commit.hexsha)):
@@ -1867,15 +1929,12 @@ if __name__ == '__main__':
1867 fetch_parser = subparsers.add_parser( 1929 fetch_parser = subparsers.add_parser(
1868 'fetch', help="Fetch a task's branch") 1930 'fetch', help="Fetch a task's branch")
1869 fetch_parser.add_argument( 1931 fetch_parser.add_argument(
1870 'task', metavar='<T123>', nargs='?', 1932 'task_or_revision', metavar='<(T|D)123>', nargs='?',
1871 help="The task to fetch") \ 1933 help="The task or revision to fetch") \
1872 .completer = DisabledCompleter 1934 .completer = DisabledCompleter
1873 1935 fetch_parser.add_argument(
1874 checkout_parser = subparsers.add_parser( 1936 '--checkout', "-c", action="store_true",
1875 'checkout', help="Checkout a task's branch") 1937 help="Also checks out the commits in a branch.") \
1876 checkout_parser.add_argument(
1877 'task_or_revision', metavar='<T123>', nargs='?',
1878 help="The task or revision to checkout") \
1879 .completer = DisabledCompleter 1938 .completer = DisabledCompleter
1880 1939
1881 browse_parser = subparsers.add_parser( 1940 browse_parser = subparsers.add_parser(