Skip to content

Commit e78a2bc

Browse files
committed
cu diff
- Add support for `cu diff` - Only show logs and diffs for changes that happened in the environment - Move `cu log` code into Repository - Add `cu log -p` and don't show patches by default - Show notes in `cu log` Signed-off-by: Andrea Luzzardi <andrea@luzzardi.com>
1 parent ac76bf2 commit e78a2bc

File tree

4 files changed

+118
-11
lines changed

4 files changed

+118
-11
lines changed

cmd/cu/diff.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"os"
5+
6+
"github.com/dagger/container-use/repository"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
var diffCmd = &cobra.Command{
11+
Use: "diff <env>",
12+
Short: "Show changes between the environment and the local branch",
13+
Args: cobra.ExactArgs(1),
14+
ValidArgsFunction: suggestEnvironments,
15+
RunE: func(app *cobra.Command, args []string) error {
16+
ctx := app.Context()
17+
18+
// Ensure we're in a git repository
19+
repo, err := repository.Open(ctx, ".")
20+
if err != nil {
21+
return err
22+
}
23+
24+
return repo.Diff(ctx, args[0], os.Stdout)
25+
},
26+
}
27+
28+
func init() {
29+
rootCmd.AddCommand(diffCmd)
30+
}

cmd/cu/log.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package main
22

33
import (
44
"os"
5-
"os/exec"
6-
"strings"
75

86
"github.com/dagger/container-use/repository"
97
"github.com/spf13/cobra"
@@ -18,22 +16,18 @@ var logCmd = &cobra.Command{
1816
ctx := app.Context()
1917

2018
// Ensure we're in a git repository
21-
if _, err := repository.Open(ctx, "."); err != nil {
19+
repo, err := repository.Open(ctx, ".")
20+
if err != nil {
2221
return err
2322
}
2423

25-
env := args[0]
26-
// prevent accidental single quotes to mess up command
27-
env = strings.Trim(env, "'")
28-
cmd := exec.CommandContext(app.Context(), "git", "log", "--patch", "container-use/"+env)
29-
cmd.Stderr = os.Stderr
30-
cmd.Stdin = os.Stdin
31-
cmd.Stdout = os.Stdout
24+
patch, _ := app.Flags().GetBool("patch")
3225

33-
return cmd.Run()
26+
return repo.Log(ctx, args[0], patch, os.Stdout)
3427
},
3528
}
3629

3730
func init() {
31+
logCmd.Flags().BoolP("patch", "p", false, "Generate patch")
3832
rootCmd.AddCommand(logCmd)
3933
}

repository/git.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,33 @@ func (r *Repository) addGitNote(ctx context.Context, env *environment.Environmen
235235
return r.propagateGitNotes(ctx, gitNotesLogRef)
236236
}
237237

238+
func (r *Repository) currentUserBranch(ctx context.Context) (string, error) {
239+
return RunGitCommand(ctx, r.userRepoPath, "branch", "--show-current")
240+
}
241+
242+
func (r *Repository) mergeBase(ctx context.Context, env *environment.EnvironmentInfo) (string, error) {
243+
currentBranch, err := r.currentUserBranch(ctx)
244+
if err != nil {
245+
return "", err
246+
}
247+
currentBranch = strings.TrimSpace(currentBranch)
248+
envGitRef := fmt.Sprintf("%s/%s", containerUseRemote, env.ID)
249+
mergeBase, err := RunGitCommand(ctx, r.userRepoPath, "merge-base", currentBranch, envGitRef)
250+
if err != nil {
251+
return "", err
252+
}
253+
return strings.TrimSpace(mergeBase), nil
254+
}
255+
256+
func (r *Repository) revisionRange(ctx context.Context, env *environment.EnvironmentInfo) (string, error) {
257+
mergeBase, err := r.mergeBase(ctx, env)
258+
if err != nil {
259+
return "", err
260+
}
261+
envGitRef := fmt.Sprintf("%s/%s", containerUseRemote, env.ID)
262+
return fmt.Sprintf("%s..%s", mergeBase, envGitRef), nil
263+
}
264+
238265
func (r *Repository) commitWorktreeChanges(ctx context.Context, worktreePath, name, explanation string) error {
239266
status, err := RunGitCommand(ctx, worktreePath, "status", "--porcelain")
240267
if err != nil {

repository/repository.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"io"
78
"log/slog"
89
"os"
910
"os/exec"
@@ -359,3 +360,58 @@ func (r *Repository) Checkout(ctx context.Context, id string) (string, error) {
359360

360361
return branch, err
361362
}
363+
364+
func (r *Repository) Log(ctx context.Context, id string, patch bool, w io.Writer) error {
365+
envInfo, err := r.Info(ctx, id)
366+
if err != nil {
367+
return err
368+
}
369+
370+
logArgs := []string{
371+
"git",
372+
"log",
373+
fmt.Sprintf("--notes=%s", gitNotesLogRef),
374+
}
375+
376+
if patch {
377+
logArgs = append(logArgs, "--patch")
378+
}
379+
380+
revisionRange, err := r.revisionRange(ctx, envInfo)
381+
if err != nil {
382+
return err
383+
}
384+
385+
logArgs = append(logArgs, revisionRange)
386+
387+
cmd := exec.CommandContext(ctx, "git")
388+
cmd.Args = logArgs
389+
cmd.Stdout = w
390+
391+
return cmd.Run()
392+
}
393+
394+
func (r *Repository) Diff(ctx context.Context, id string, w io.Writer) error {
395+
envInfo, err := r.Info(ctx, id)
396+
if err != nil {
397+
return err
398+
}
399+
400+
diffArgs := []string{
401+
"git",
402+
"diff",
403+
}
404+
405+
revisionRange, err := r.revisionRange(ctx, envInfo)
406+
if err != nil {
407+
return err
408+
}
409+
410+
diffArgs = append(diffArgs, revisionRange)
411+
412+
cmd := exec.CommandContext(ctx, "git")
413+
cmd.Args = diffArgs
414+
cmd.Stdout = w
415+
416+
return cmd.Run()
417+
}

0 commit comments

Comments
 (0)